InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

Wordpress performance

  • Embed youtube video with on-click lazy loading
  • How to Preload cache in wordpress on Linux
  • How to remove jquery-migrate.js from wordpress
  • How to use wordpress facebook plugin comments box with lazy loading
  • Performance tips for Wordpress site
  • w3 total cache for wordpress
 
  • Home
  • > Tutorials
  • > Wordpress
  • > Wordpress Performance

Embed youtube video with javascript on-click lazy loading approach

By admin | Last updated on Apr 3, 2016

Embedding youtube video using the default iframe code fetches couple of files and makes the page load little slower for the end user. Many users may not actually play the video. It also loads a flash file on the page even if we don’t not play the video.

Default YouTube embed code

Here is how the default embed code looks like:

<iframe width="420" height="315" src="http://www.youtube.com/embed/1B-vb6yZBI8" frameborder="0" allowfullscreen></iframe>

It fetches couple of pages. Some of these are:

  1. iframe main page: e.g. http://www.youtube.com/embed/NxJ0W-yz0LQ
  2. Css and Javascript files from YouTube.
  3. A flash (swf) file
  4. Video thumbnail and few other files

Embedding YouTube video using lazy loading

In this solution we just embed the thumbnail of the video and display a play button or something similar which on clicking load all the required pages/files from YouTube and startes the video. Here are the steps for this:

  1. First insert this code wherever you want to display the video image with play option.
    <div id="youtubeembed" id="gdXN0DXMd6Y"></div>
  2. Insert jquery.embedyt.js javascript code at the end of your html document.
    /*jslint indent: 2 */
    /*!
     * EmbedYt 1.0
     * Author : http://infoheap.com/
    */
    (function ($) {
      "use strict";
    
      $.fn.embedyt = function (youid) {
        var htm = '<div id="player' + youid + '"' + ' style="background-image:url(' + 'http://img.youtube.com/vi/' + youid + '/0.jpg' + ');' + 'width:480px;height:360px;text-align:center;line-height:360px;' +  '">' + '<input style="cursor:pointer;" type="button" value=" play " />' + '</div>';
        this.html(htm);
        this.find("div input[type=button]").bind('click', function () {
          var ifhtml = '<iframe width="420" height="315" src="http://www.youtube.com/embed/' + youid + '?autoplay=1" frameborder="0" allowfullscreen></iframe>';
          jQuery(this).css("cursor", "progress");
          jQuery(this).parent().parent().html(ifhtml);
        });
      };
    
    }(window.jQuery||window.$));
    
    Since the code is small it is better to insert it as inline javascript to avoid one extra round trip to fetch the javascript file from the server. Other option is to insert is as external javascript file include.
  3. After above script code (jquery.embedyt.js), insert the following code to insert YouTube video thumbnail image and link into the video container div.
    jQuery(function () {
      jQuery("div.youtubeembed").each(function (index) {
        jQuery(this).embedyt(jQuery(this).attr('id'));
      });
    });

    Note that the above code runs after the html document has been loaded and is ready. The first line where we pass a function to jQuery ensures that.

  4. Here is how it looks when it renders in browser. You can also see a live example in this post.
    youtube-embed-lazy-loading-using-js-exampleIf you want to use some image instead of play button, you can make small modification in the code and replace button with image.
  5. In case you are using wordpress, you can add an action handler on wp_footer and embed the javascript for jquery.embed.js and the call to embedyt. Here is high level pseudocode:
    function my_inline_footer_js() {
    // echo javascript here
    }
    add_action('wp_footer', 'my_inline_footer_js');

Suggested posts:

  1. How to create and embed youtube playlist
  2. How to use w3 total cache for wordpress
  3. How to become Google verified author of your wordpress blog using Google+ profile
  4. Python selenium webdriver – quick start guide on Mac
  5. How to create youtube video link from a specific start time
  6. How to use Google custom search with lazy loading approach
  7. Ubuntu – check if a service is upstart based
  8. How to create vanity url for your channel in youtube
Share this article: share on facebook share on linkedin tweet this submit to reddit
Posted in Tutorials | Tagged Javascript, Site Performance, Tutorials, Wordpress, Wordpress Performance, Youtube
  • Browse content
  • Article Topics
  • Article archives
  • Contact Us
Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

Copyright © 2025 InfoHeap.

Powered by WordPress