• I want to add a code with the plugin “code snippets” that you recomend, but when I try to do it, it gives an error, could you please help me telling me where I should place it (header, foooter) and how I should write it.

    This is to load the preview thumbnail of the video, and only load the video when the user clicks on the image.

    This is the code:

    ( function() {
    
      var youtube = document.querySelectorAll( ".youtube" );
      
      for (var i = 0; i < youtube.length; i++) {
        
        var source = "https://img.youtube.com/vi/"+ youtube[i].dataset.embed +"/sddefault.jpg";
        
        var image = new Image();
            image.src = source;
            image.addEventListener( "load", function() {
              youtube[ i ].appendChild( image );
            }( i ) );
        
            youtube[i].addEventListener( "click", function() {
    
              var iframe = document.createElement( "iframe" );
    
                  iframe.setAttribute( "frameborder", "0" );
                  iframe.setAttribute( "allowfullscreen", "" );
                  iframe.setAttribute( "src", "https://www.youtube.com/embed/"+ this.dataset.embed +"?rel=0&showinfo=0&autoplay=1" );
    
                  this.innerHTML = "";
                  this.appendChild( iframe );
            } );  
      };
      
    } )();

    I get this code from this article: https://webdesign.tutsplus.com/tutorials/how-to-lazy-load-embedded-youtube-videos–cms-26743

    Thank you.

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Where and how I should add this code?’ is closed to new replies.