Lazy Load IFRAMEs

By  on  

We've known for a decade that lazy loading resources like JavaScript, CSS, and especially images is a massive performance win for web pages. At first we used tricks and JavaScript to do the lazy loading, but more recently native image lazy loading has debuted in browsers.

Did you know that you can also lazy load IFRAMEs using the same loading="lazy" attribute and value?

<iframe 
    src="https://davidwalsh.name/"
    loading="lazy"
    onload="alert('Loaded!');"
/>

You can see how lazy loading IFRAMEs works with this demo:

See the Pen IFRAME Lazy Load by David Walsh (@darkwing) on CodePen.

This single attribute to perform a complex but useful operation is the ideal solution for lazy loading just about anything. I'm so thankful that browsers are implementing APIs that make using best practices so easy!

Recent Features

  • By
    Designing for Simplicity

    Before we get started, it's worth me spending a brief moment introducing myself to you. My name is Mark (or @integralist if Twitter happens to be your communication tool of choice) and I currently work for BBC News in London England as a principal engineer/tech...

  • By
    5 Ways that CSS and JavaScript Interact That You May Not Know About

    CSS and JavaScript:  the lines seemingly get blurred by each browser release.  They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely.  We have our .js files and our .css, but...

Incredible Demos

  • By
    TextboxList for MooTools and jQuery by Guillermo Rauch

    I'll be honest with you: I still haven't figured out if I like my MooTools teammate Guillermo Rauch. He's got a lot stacked up against him. He's from Argentina so I get IM'ed about 10 times a day about how great Lionel...

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

Discussion

  1. This browser feature looks really like a huge gamechanger. Would love so much to use it with profit although it doesn’t seem to work correctly for me.

    [First of all, using it with images instead of iframes always worked perfectly, so all this regards only loading=”lazy” for iframes.]

    After hours of testing on different pages, what I see is that using loading=”lazy”, Iframes load WAY TOO EARLY.

    This apparently buggy behavior happens only if the page itself is NOT loaded from another iframe. So this rules out CodePen as a viable tool to show this.

    Let’s make a text.html file from this code, almost identical to your example, to highlight exactly what I’m saying:
    https://gist.github.com/jeff-at-livecanvas/bb1a803465419c62d623305daf6722cd

    Opening this file in a Chrome window, with an internal height of 700 pixels, shows the Loaded! message immediately.
    Same happens with latest Firefox too.

    Would be great if somebody could shed some light on this.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!