When we use of videos Like Youtube, Wistia, Vimeo, etc. Webpage speed will suffer a lot. Google has recently announced Google Search ranking is purely based on the PageSpeed.

Here is easy way to defer your video that uses scripts.

Below is sample YouTube video Embed Link :

<iframe width="560" height="315" src="//www.youtube.com/embed/OMOVFvcNfvE" frameborder="0" allowfullscreen></iframe>

Change the above code a bit to apply a trick.

<iframe width="560" height="315" src="" data-src="//www.youtube.com/embed/OMOVFvcNfvE" frameborder="0" allowfullscreen></iframe>

Now we need to use small Javascript code to load the video without any issue.

<script>
function init() {
var vidDefer = document.getElementsByTagName('iframe');
for (var i=0; i<vidDefer.length; i++) {
if(vidDefer[i].getAttribute('data-src')) {
vidDefer[i].setAttribute('src',vidDefer[i].getAttribute('data-src'));
} } }
window.onload = init;
</script>

above javascript code will set the src=”” URL from copying the original video link from data-src=”” attribute.

That’s It. Now your page will load even faster.

 

 

 

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *