• Skip to content
  • Skip to primary sidebar
  • About
  • Contact
  • Resources
  • Blog
    • Easy Digital Downloads
    • General
    • Genesis Framework
    • jQuery/JavaScript
    • Project Management
    • WordPress Functions
    • WordPress Plugin Development
  • Army Lessons

Scott DeLuzio

WordPress Development Tutorials

Defer Parsing of JavaScript With YouTube Videos

October 30, 2017 Scott DeLuzio 22 Comments

Deferring parsing of JavaScript is one of those messages that you get on sites like GTmetrix and other page speed sites that may sound like it’s from another planet. What on Earth does defer parsing of JavaScript even mean? Basically, what the message is saying is that a browser needs to process all of the content inside <script> tags before it can load the page. By putting off the processing of JavaScript until it is actually needed, you can reduce the initial load time of your page.

Defer Parsing of JavaScript YouTube Videos

I had a site with one YouTube video on the home page. The home page, when loaded was getting a low score in GTmetrix, and I wanted to do something to change it. The worst offender was Defer Parsing Of JavaScript, which had 13 scripts being loaded on the initial page load. Six of those were from youtube.com!

When you embed a video in WordPress by copying and pasting the video’s URL from sites like YouTube, Vimeo, etc. WordPress creates an <iframe> for that video. It’s basically the same <iframe> code that you’d get by copying the embed code directly from YouTube.

So, instead of using the URL to embed the video, go to YouTube and copy the <iframe> embed code.

  • Under the video, click the Share button
  • Towards the bottom, click the Embed button
  • Make any adjustments to the Embed Options you may need
  • Copy the embed code at the top that starts with <iframe width=

Next, come back to your site and paste that <iframe> code where you want it to go on your site.

You’re going to need to change a bit of the <iframe> code.

  • Find the src="https://www.youtube.com... and change src to data-src
  • Add another empty parameter src="". This replaces the src we just changed to data-src

Your <iframe> should look something like this now:

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

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

The JavaScript

The <iframe> we set up by itself won’t load the video correctly. We’ll use some JavaScript to get it to load correctly.

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

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

The next time I ran the GTmetrix test on that page, the Defer Parsing of JavaScript score went from an F (0) to a B (85). Initially, there was 2.2MB of JavaScript being loaded during the initial page load. After optimizing, it was down to about 168KB.
Defer Parsing of JavaScript With YouTube Videos

Other Considerations

In my case the video was only on the home page, so I made sure the JavaScript file only loaded on the home page. In the future, if I have other pages that need this adjustment, I can add them to this conditional in my wp_enqueue_scripts function. If the video is set up the same way, you shouldn’t need any other JavaScript to load other videos on your site as the JavaScript looks for any <iframe> elements that have a data-src attribute.

Also, special thanks to Patrick Sexton, for the initial guidance for this post.

jQuery/JavaScript

Reader Interactions

Comments

  1. jonatan says

    November 18, 2017 at 2:27 pm

    Hi, it worked for me but the video stopped beeing responsive.
    Is there a way to keep responsiveness?

    Reply
    • Scott DeLuzio says

      November 22, 2017 at 8:38 am

      You can do that with some CSS. Check out this tutorial for some guidance: https://benmarshall.me/responsive-iframes/

      Reply
      • Samuel Clark says

        June 14, 2018 at 3:27 pm

        Where does the Css from this tutorial from Ben go?

        Reply
        • Scott DeLuzio says

          July 23, 2018 at 11:14 am

          You would need to add that to either (1) your child theme’s style.css, (2) the Appearance > Customize > Additional CSS, or (3) in a custom stylesheet in a plugin. #2 is probably the easiest place to put it.

          Reply
  2. Pranshu Kharkwal says

    January 30, 2018 at 10:12 am

    Where do I need to paste this Javascript Code?

    Reply
    • Scott DeLuzio says

      January 31, 2018 at 9:50 am

      You would need to add it to it’s own JS file and enqueue it on your pages as needed. Check out the codex source for how to do this.

      Reply
  3. Joe says

    March 26, 2018 at 11:21 am

    Hello im having issues with doing this where i can now not pause the video?

    Reply
  4. cesare says

    March 27, 2018 at 6:23 am

    HI Scott,
    Thanks for the very helpful script.

    I added it to our homepage with the Google tagmanager. In about 50% of the cases, i.e. page reloads it doesn’t work: blank space where the videoplayer should appear (to wait further doesn’t help…). Any idea what I am doing wrong. Page load speed drops from 0.952 s to 0.781 s with your script. Thats cool actually.

    Reply
  5. Paul says

    May 6, 2018 at 1:11 pm

    Great trick! Thanks a lot!

    Reply
  6. ROGER GIOVANNY GUERRERO LEON says

    June 30, 2018 at 1:54 pm

    Hi can you please tell me which is the file where I have to paste the last Javascript code. I am not so good about coding

    Thanks

    Reply
    • Scott DeLuzio says

      July 23, 2018 at 11:09 am

      You will have to create a JS file and include it in your plugin with the wp_enqueue_scripts function.

      Reply
  7. Kanewilliam says

    August 3, 2018 at 7:38 am

    My site home page have 4 youtube videos

    only first iframe src apply working other three iframe src value is “unknown”.

    Reply
    • Scott DeLuzio says

      August 15, 2018 at 12:45 pm

      I did this with only one video on the page, so I can’t say for sure how it will work with multiple videos. But you can try something like this (untested) code:

      function init() {
      var vidDefer = document.getElementsByTagName('iframe');
      var iframes = Array.prototype.slice.call(vidDefer);
      iframes.forEach(function(iframe) {
      if(iframe.getAttribute('data-src')) {
      iframe.setAttribute('src',iframe.getAttribute('data-src'));
      }
      }
      }
      window.onload = init;

      Reply
  8. Dan says

    August 11, 2018 at 11:14 am

    Incredible advice. Changed the two YouTube videos on the home page of my site and just went from an ‘F’ grade in defer parsing of JavaScript on GT Metrix for a 100% ‘A’. Can’t thank you enough.

    Now – next question. I have over 200 other YouTube videos on my site. Do I have to change each of those individually to help the load speed for those (if I’m sending people to those individual pages via social media) or is there a better way?

    Reply
    • Scott DeLuzio says

      August 15, 2018 at 12:38 pm

      You shouldn’t need to add the JavaScript for each video (the one time is sufficient), but the src="" data-src="https://youtube.com/..." bit will need to be modified for each video to get the benefit.

      Reply
  9. Asdeideas Diseño says

    August 13, 2018 at 1:05 am

    Hi! First thing I’ve tried to defer parsing of JavaScript was to activate the option in the cache plugin I use (Litespeed Cache), but I had some visualization problems about Rev Slider and translation plugin also (Transposh), so I excluded the corresponding JS of deferring, but not fixing the error… After that, did the same but using Fast Velocity Minify plugin instead, same results. Maybe adding the code to functions.php skipping these problematic JS could be the solution but, how can I exclude it by code?

    Many thanks for the help!

    Reply
  10. Kelvin Akpe says

    October 2, 2018 at 3:31 pm

    Great! It worked for me. Saved me the trouble

    Reply
    • Scott DeLuzio says

      October 2, 2018 at 4:05 pm

      Glad it worked for you Kelvin!

      Reply
  11. Zuphel Baguio says

    October 16, 2018 at 1:13 am

    Hi Scott,

    Great tutorial. But not working on Firefox. I mean, no display.

    Reply
    • Scott DeLuzio says

      October 16, 2018 at 10:22 am

      Try clearing the browser’s cache and use a Private Window (CTRL + Shift + P) so no browser extensions are conflicting with what’s on the page.
      It should work on any browser.

      Reply
  12. Rizwan Wris says

    February 5, 2019 at 8:43 pm

    You did not mention for newbies where to paste that Java Script Code ?
    in tag or after tag?
    Where?

    Reply
    • Scott DeLuzio says

      February 6, 2019 at 10:00 am

      You could place that in a separate JavaScript file and use the WordPress wp_enqueue_script() function to load it as needed.

      Some themes also have a way to add scripts to individual pages, which should work as well.

      Reply

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Come say hi!

  • Facebook
  • GitHub
  • Instagram
  • LinkedIn
  • Twitter
  • YouTube

Tweets by scottdeluzio

My Products

Conditional Checkout FieldsFull Screen Background ImagesPrivacy WPQuick CheckoutWP1099WP-CRM System

I use affiliate links throughout this site and may earn a commission if you purchase through my links. I do not link to products or services that I do not trust, or use myself.
© 2019 · Scott DeLuzio · Built on the Genesis Framework