Answers for "defer iframe"

1

How To Defer Iframe

// Move src to data-src
<iframe src="" data-src="https://www.youtube.com/embed/JdyDz6quE18"></iframe>


// Add this script
function deferIframe() {
  var iframeElem = document.getElementsByTagName('iframe');
  for ( var i = 0; i < iframeElem.length; i++ ) {
    if(iframeElem[i].getAttribute('data-src')) {
      iframeElem[i].setAttribute('src',iframeElem[i].getAttribute('data-src'));
    } 
  } 
}
window.onload = deferIframe;
Posted by: Guest on August-07-2020

Browse Popular Code Answers by Language