I am looking for something similar to jQuery image lazy load plugin, but for iframes.
When implemented, this feature will allow lazy loading when the iframe enters the viewport without any JavaScript!
There is also an analogous feature for images: How do you make images load lazily only when they are in the viewport?
For iframes, I'm not sure what is the status, but it does seem like there is an intention to enable it as well:
loading currently, while https://html.spec.whatwg.org/multipage/embedded-content.html#the-img-element doesTo observe this in action, open the "Network" tab of your developer tools before running the snippet below. If your browser implements the feature, you can clearly see new network requests as you scroll down to the next iframe!
Test outcomes:
document.getElementById('load-now').addEventListener('click', function(){
for (const img of document.getElementsByTagName('iframe')) {
img.loading = 'eager';
}
});
.separator {
height: 1000px;
width: 100px;
border: 5px solid red;
}
img {
height: 340px;
border: 5px solid black;
}
#load-now {
border: 5px solid black;
}
Click me to load all images now!
YouTube videos
For YouTube embedded video iframe more specifically: Lazy Load youtube video from iframe API
On Chromium 81, it also works on jsfiddle: https://jsfiddle.net/cirosantilli/3p0tk5nc but didn't work here on Stack Overflow. I'm not sure why. Removing the loading="lazy" makes no difference, so I think it fails for some other reason.
Keep in mind that embedding YouTube videos on a local HTML file also fails randomly to me which makes testing harder: Embed Youtube code is not working in HTML
document.getElementById('load-now').addEventListener('click', function(){
for (const img of document.getElementsByTagName('iframe')) {
img.loading = 'eager';
}
});
.separator {
height: 1000px;
width: 100px;
border: 5px solid red;
}
img {
height: 340px;
border: 5px solid black;
}
#load-now {
border: 5px solid black;
}
Click me to load all images now!