Free mobile recharge for 10 lucky winners participate in this giveaway
HackingUniversity - Hacks . Tricks . How-To's

08 May 2016

How to Embed YouTube Videos with Optimized Loading

Embedding YouTube videos in any webpage is great, it enhances your content and helps your reader to view your content in a much broader way. Either you are a blogger or just a website creator embedding YouTube videos in your blog is a must step for engaging good readership, as they need a nice way to learn new things.

embed youtube videos in best oprimized way to reduce blogs loading time

Now you face a lot of issues while embedding a YouTube video, if the person’s internet speed is slow then YouTube videos might not load parallel to all that content. So he won’t be able to see content and hence he will leave your blog that will effect the bounce rate of your blog and decrease SEO rankings.

Embedding YouTube Videos Responsibly to Reduce Blog’s Loading Speed

Today we are going to solve that slow YouTube videos loading problem and hence embed all the videos from YouTube responsibly or hence better word to say would be in optimized way to increase your blog’s loading speed.

What we will be doing is to implement a script created by Amit Agarwal from that will fetch on the the first image from the video and will load the entire video on demand. Yes with this if the person really wants to see the video he will click the video and then original YouTube player will get loaded.

For demo purpose, check below video I have embedded using this script. You just need to click the below video to get it fully loaded and then you can watch it easily.

Embed YouTube Videos on Blogger without Decreasing Blog’s Loading Time

So lets check out how to solve that slow youtube video loading option and embed youtube videos responsibly on your blogger, wordpress or any other website easily.

  1. Open Blogger > Template > Edit HTML.
  2. Press Ctrl + F and search for ]]></b:skin> tag and paste below CSS styling above it. If you have removed default CSS bundles from blogger then you need to paste the code above </style> tag.

    .youtube-container{overflow:hidden;display:block;margin:20px auto;width:100%}.youtube-player{display:block;width:100%;padding-bottom:56.25%;overflow:hidden;position:relative;width:100%;height:100%;cursor:hand;cursor:pointer;display:block}{bottom:0;display:block;left:0;margin:auto;max-width:100%;width:100%;position:absolute;right:0;top:0;height:auto;transition:all .2s ease-in-out}{transform:scale(1.1)}{height:75px;width:75px;left:50%;top:50%;margin-left:-36px;margin-top:-36px;position:absolute;background:url(&quot;;) no-repeat}#youtube-iframe{width:100%;height:100%;position:absolute;top:0;left:0}

  3. Now your CSS styling is pasted, its time to add the script that will do the whole work.
  4. Once again press Ctrl + F and search for </body> tag and paste the below script above it.

    <script src=''/>

    <script>(function(){var v=document.getElementsByClassName(&quot;youtube-player&quot;); for (var n=0; n &lt; v.length; n++){var p=document.createElement(&quot;div&quot;); p.innerHTML=tricksladderThumb(v[n]; p.onclick=tricksladderIframe; v[n].appendChild(p);}})(); function tricksladderThumb(id){return &#39;&lt;img class=&quot;youtube-thumb&quot; src=&quot;//; + id + &#39;/hqdefault.jpg&quot;&gt;&lt;div class=&quot;play-button&quot;&gt;&lt;/div&gt;&#39;;}function tricksladderIframe(){var iframe=document.createElement(&quot;iframe&quot;); iframe.setAttribute(&quot;src&quot;, &quot;//; + + &quot;?autoplay=1&amp;autohide=2&amp;border=0&amp;wmode=opaque&amp;enablejsapi=1&amp;controls=1&amp;modestbranding=1&quot;); iframe.setAttribute(&quot;frameborder&quot;, &quot;0&quot;); iframe.setAttribute(&quot;id&quot;, &quot;youtube-iframe&quot;); this.parentNode.replaceChild(iframe, this);}</script>

  5. If you already use jQuery then remove the above RED line from the script else leave it with the script, as it is required for the full working of the script.
  6. Now save your template and all the codes are pasted for this trick to work, now you just need the embed code that you will be using in every post for showing your videos.

    <div class="youtube-container"><div class="youtube-player" data-id="xxxxxxxxxxxx"></div></div>

  7. Everytime you need to show any YouTube video in any post instead of normal Embed or iFrame code, you need to paste the above code and make sure to change the RED XXXXX with the video code.

That's it, now your youtube videos are embedded properly and will not slow down your blog post while its loading. With this you can embed any number of videos in your blog and all the videos will only load when user wants them too.