Skip to content

Instantly share code, notes, and snippets.

@eduard-un
Created January 11, 2021 10:48
Show Gist options
  • Save eduard-un/ff0e9335e7cf299f70cf3bf396852af1 to your computer and use it in GitHub Desktop.
Save eduard-un/ff0e9335e7cf299f70cf3bf396852af1 to your computer and use it in GitHub Desktop.
Js code for preloading Youtube Videos in Divi
( function() {
setTimeout( 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 );
} );
};
}, 100 );
} )();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment