loading="lazy" attribute polyfill
srcset and within
picture, as well as
loading="lazy" will be a huge improvement for todays web performance challenges, so use and polyfill it today!
- Released under the MIT license
- Made in Germany. And supported by so many great people from all over this planet - see "Credits" accordingly.
- Compatible down to Microsoft Internet Explorer 9
- Lightweight (see the badge above)
- Web standards: supports the standard
- Performance: it's based on highly efficient, best practice code.
- SEO & crawlers: the image and iframe contents aren't being hidden from crawlers that aren't capable of scrolling.
The polyfill was designed with the following concepts kept in mind:
You may optionally load via NPM or Bower:
$ npm install loading-attribute-polyfill $ bower install loading-attribute-polyfill
You could even load the polyfill asynchronously: https://output.jsbin.com/codelib/1
Afterwards you have need to wrap all of your
<img> (and in case of
<picture> usage the complementary
<source> HTML tags as well) and
<iframe> HTML tags that you'd like to lazy load (and thatfor added a
loading="lazy" attribute as well) by an
<noscript> HTML tag.
Please keep in mind that it's beneficial to even also include
height attributes on
<img> HTML tags, as the browser could determine the aspect ratio via those two attributes values being set (even if you overwrite them via CSS), compare to the great work by Jen Simmons on this topic, e.g. within these articles https://css-tricks.com/do-this-to-improve-image-loading-on-your-website/ (with video) or https://css-tricks.com/what-if-we-got-aspect-ratio-sized-images-by-doing-almost-nothing/
Image wrapped in a picture tag
Optional additional dependencies
In case you'd like to support older versions of Microsoft Edge, Microsoft Internet Explorer 11 or Apple Safari up to 12.0, you could (conditionally) load an IntersectionObserver polyfill:
Nevertheless this polyfill would still work in those browsers without that other polyfill included, but this small amount of users wouldn't totally benefit from the lazy loading functionality - we've at least got you partly covered by using the Microsoft proprietary lazy loading resource hints.
Internet Explorer 9 & Internet Explorer 10
Internet Explorer 9 and 10 have bugs where the 'interactive' state can be fired too early before the document has finished parsing.
That for you would need to include the polyfill the latest within the HTML code, like the nearest to the closing
body HTML tag, as including it e.g. within the
head section might lead to an unexpected state, so that in worst case the images might not get loaded.
Internet Explorer 9
The polyfill has been enhanced to even also provide it's functionality on IE9. But please keep in mind to even also include a
And the images are still displaying an error in the demo on IE9, as most likely (from my understanding) this browser doesn't work with the HTTPS protocol any more, but the src-attributes values are correctly rewritten after all.
Nothing really, just integrate it as shown within the "installation" section, and it
will should work out of the box.
See the polyfill in action either by downloading / forking this repo and have a look at
demo/index.html, or at the hosted demo: https://mfranzke.github.io/loading-attribute-polyfill/demo/
Further implementations - Kudos for that
Nico23 has developed a WordPress plugin: https://wordpress.org/plugins/native-lazyload-polyfill/ (which is much better than the one by Google !)
PHP Twig Extension
@tim-thaler has developed a PHP Twig Extension: https://github.com/tim-thaler/twig-loading-lazy
Craft Twig Loading Lazy plugin
@tim-thaler has even also developed a Craft Twig Loading Lazy plugin: https://github.com/tim-thaler/craft-twig-loading-lazy
Credits for the initial kickstarter / script to @Sora2455 for better expressing my ideas & concepts and support by @cbirdsong, @eklingen, @DaPo, @nextgenthemes, @diogoterremoto, @dracos, @Flimm, @TomS- and @vinyfc93. Thank you very much for that, highly appreciated !
- macOS 10.14, Mozilla Firefox 68.0.1 (manually, localhost)
- macOS 10.14, Safari 12 (via CrossBrowserTesting)
- macOS 10.13, Safari 11 (via CrossBrowserTesting)
- iPad 6th Generation Simulator, Mobile Safari 12.0 (via CrossBrowserTesting)
- Windows 10, Google Chrome / versions latest & latest-1 (via CrossBrowserTesting)
- Windows 10, Microsoft Edge / versions 17, 18 (via CrossBrowserTesting)
- Windows 10, Microsoft Internet Explorer / version 11 (via CrossBrowserTesting) - Windows 7 SP1, Internet Explorer 9.0.8112.16421 (manually, localhost)
Cross-browser testing platform provided by CrossBrowserTesting
Things to keep in mind
- The HTML demo code is meant to be simple
- This polyfill doesn't (so far) provide any functionality for the
loading="eager"value, as this was released even already, but still seems to be in the measure, learn and improvements phase.
More information on the standard
If you're trying out and using my work, feel free to contact me and give me any feedback. I'm curious about how it's gonna be used.
And if you do like this polyfill, please consider even also having a look at the other polyfill we've developed: https://github.com/mfranzke/datalist-polyfill/