Standardized lazy-loading for images landed in Chrome 76 via the
Show
Standardized lazy-loading of iframes defers offscreen iframes from being loaded until the user scrolls near them. This saves data, speeds up the loading of other parts of the page, and reduces memory usage. This demo of `<iframe loading=lazy>`shows lazy-loading video embeds: Browser compatibilityBrowser Support
Browsers that do not support the Why should we lazy-load iframes?Third-party embeds cover a wide range of use cases, from video players, to social media posts, to ads. Often this content is not immediately visible in the user's viewport. Rather, it's only seen once they scroll further down the page. Despite this, users pay the cost of downloading data and costly JavaScript for each frame, even if they don't scroll to it. Based off Chrome's research into automatically lazy-loading offscreen iframes for Data Saver users, lazy-loading iframes could lead to 2-3% median data savings, 1-2% First Contentful Paint reductions at the median, and 2% First Input Delay (FID) improvements at the 95th percentile. Additionally, lazy-loading off-screen iframes can impart benefits to Largest Contentful Paint (LCP). , such as images or text dependent on web fonts in order to render. Because iframes can often require a significant amount of bandwidth in order to load all of their subresources, lazy-loading offscreen iframes can significantly reduce bandwidth contention on network-constrained devices, leaving more bandwidth to load resources which contribute to a page's LCP. How does built-in lazy-loading for iframes work?The
Using the
Not specifying the attribute at all will have the same impact as explicitly eagerly loading the resource. If you need to dynamically create iframes via JavaScript, setting
5 on the element is also supported:
What impact might we see from lazy-loading popular iframe embeds?What if we could change the web at large so that lazy-loading offscreen iframes was the default? It would look a little like this: Lazy-loading YouTube video embeds (saves ~500KB on initial page load):
Anecdote: when we switched to lazy-loading YouTube embeds for Chrome.com, we saved 10 seconds off of how soon our pages could be interactive on mobile devices. I have opened an internal bug with YouTube to discuss adding
6 to its embed code. Lazy-loading Instagram embeds (saves >100KB gzipped on initial load): Instagram embeds provide a block of markup and a script, which injects an iframe into your page. Lazy-loading this iframe avoids having to load all of the script necessary for the embed. Given such embeds are often displayed below the viewport in most articles, this seems like a reasonable candidate for lazy-loading of their iframe. Lazy-loading Spotify embeds (saves 514KB on initial load):
Although the above embeds illustrate the potential benefits to lazy-loading iframes for media content, there's the potential to also see these benefits for ads. Facebook's social plugins allow developers to embed Facebook content in their web pages. There's a number of these plugins offered, such as embedded posts, photos, videos, comments… The most popular is the Like plugin - a button that shows a count of who has "liked" the page. By default, embedding the Like plugin in a webpage (using the FB JSSDK) pulls in ~215KB of resources, 197KB of which is JavaScript. In many cases, the plugin may appear at the end of an article or near the end of a page, so loading it eagerly when it's offscreen may be suboptimal. Thanks to engineer Stoyan Stefanov, . Developers who opt in to lazy-loading via the plugins'
7 configuration will now be able to avoid it loading until the user scrolls nearby. This enables the embed to still fully function for users that need it, while offering data-savings for those who are not scrolling all the way down a page. We are hopeful this is the first of many embeds to explore standardized iframe lazy-loading in production. Can I lazy-load iframes cross-browser? Yesiframe lazy-loading can be applied as a progressive enhancement. Browsers which support
6 on iframes will lazy-load the iframe, while the It is also possible to lazy-load offscreen iframes using the lazysizes JavaScript library. This may be desirable if you:
Use the following pattern to feature detect lazy-loading and fetch lazysizes when it's not available:
Are there instances when offscreen iframes with
6 are still loaded? An early experiment with automatically lazy-loading iframes for Data Saver users in Chrome had an exception for hidden iframes, often used for communications or analytics. These were prevented from loading lazily and always loaded to prevent breaking these capabilities. With the ConclusionBaking in standardized support for lazy-loading iframes makes it significantly easier for you to improve the performance of your web pages. If you have any feedback, please feel free to submit an issue to the Chromium Bug Tracker. And, in case you missed it, check out web.dev's for more lazy-loading ideas. With thanks to Dom Farolino, Scott Little, Houssein Djirdeh, Simon Pieters, Kayce Basques, Joe Medley and Stoyan Stefanov for their reviews. How to create an iframe from JavaScript?// Creating iframe element. var el = document. createElement("iframe"); // setting the values for the attributes. How to create dynamic iframe in JavaScript?createElement() function to create an iframe element. Then we used the appendChild() function to append it to some DOM element on our HTML page, in this case, a <div> tag with id attribute as myDIV. Then we opened the iframe and added the HTML code which we want to run inside the iframe using the document. Is iframe deprecated?Powerful, but easy to misuse They were almost always a bad approach to design. Thankfully, the <frame> element has been deprecated in HTML5, but the <iframe> , or “inline frame” is still available. What is the difference between iframe and embed?EMBED is basically the same as IFRAME, only with fewer attributes. Formally, EMBED is an HTML 5 tag, but on several browsers it will also work for HTML 4.01, if you are using this. It just cannot be validated. |