I have a page which displays multiple (usually 10) embedded videos. The videos use the new IFRAME embed code of youtube and apparently for every IFRAME there is a separate request when loading the page. Is there a way to defer loading the videos after the rest of the page is loaded, so they don't slow down page loading that much?
The embedded YouTube videos make your page heavy and slow down your page load times. The page load time is important factor for a web page because – Site Speed improves user experience. There are different surveys and case studies which show poor experience with slow sites.
A single YouTube embed can load up to 400KB of data before you even hit the play button. They also force the visitor’s browsers to make extra http requests. On a site with multiple videos embedded (like above), the extra weight can noticeably slow down load speeds (but even one embed makes a difference).
Here’s the short answer: It depends. The fact is that there are many ways to embed videos on a web page. The way you choose to go about doing it will have a significant impact on your web page load time. Here some examples of the different ways you can embed a video onto a web page today:
Nevertheless, there are ways to speed up page loading time for embedded YouTube videos. Lazy loading is one of the most popular strategies in use today. It commands users’ web browsers to only load the part of the web page currently showing on the screen. Everything else is left to load later, when (or if) the user decides to scroll further.
Well, I wrote a javascript thingy (called "LYTE") that will create a "dummy player" (which looks & feels like a normal YouTube embed) for every div with a specific class ("lyte") and id with the YouTube-id. Only when clicking the "dummy" player, the actual YouTube iframe is loaded and it indeed has an important impact of the performance of pages that embed YouTube videos. You can see it in action on my blog.
LYTE is currently not really available standalone, only as part of WP-YouTube-Lyte, the WordPress plugin I wrote, but with minimal changes you should be able to extract all relevant code from the plugin.
You basically have to create something like this in your HTML;
<div class="lyte" id="7nuiOe8M6bw" style="width:640px;height:385px;">
<script type="text/javascript"><!--
var nT='newtube-';var bU='http://static.blog.futtta.be/wp-content/plugins/wp-youtube-lyte/lyte/';
var d=document;if(d.addEventListener){d.addEventListener('DOMContentLoaded', insert, false)}else{window.onload=insert}
function insert(){if(!d.getElementById('lytescr')){lytescr=d.createElement('script');lytescr.async=true;lytescr.id='lytescr';lytescr.src='http://static.blog.futtta.be/wp-content/plugins/wp-youtube-lyte/lyte/lyte-min.js';h=d.getElementsByTagName('script')[0];h.parentNode.insertBefore(lytescr, h)}};
--></script></div>
This block will load lyte-min.js, which will fill the div with all graphical elements of the dummy player (image, play button, control bar, title) and will add an eventlistener to the div that will trigger the replacement of the div with the real embedded player when clicked.
You can find the plugin here and look at the code here (wp-youtube-lyte.php creates the div, lyte/lyte.js is the actual javascript ... thingy).
Check out this solution: Load the YouTube Video Player On-Demand
Basically, it's like replacing your YouTube video iframe player codes with jQuery when clicking some thumbnails.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With