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
    Create Namespaced Classes with MooTools

    MooTools has always gotten a bit of grief for not inherently using and standardizing namespaced-based JavaScript classes like the Dojo Toolkit does.  Many developers create their classes as globals which is generally frowned up.  I mostly disagree with that stance, but each to their own.  In any event...

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

Incredible Demos

  • By
    Select Dropdowns, MooTools, and CSS Print

    I know I've harped on this over and over again but it's important to enhance pages for print. You can do some things using simple CSS but today's post features MooTools and jQuery. We'll be taking the options of a SELECT element and generating...

  • By
    Ana Tudor&#8217;s Favorite CodePen Demos

    Cocoon I love canvas, I love interactive demos and I don't think I have ever been more impressed by somebody's work than when I discovered what Tiffany Rayside has created on CodePen. So I had to start off with one of her interactive canvas pens, even though...

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!