This is not a duplicate of this because it also uses the document.ready approach which obviously does not work.
I want to avoid that the browser loads images (<img>) nested within hidden <div> elements.
So I tried this, however the javascript gets executed too late, the browser already starts to download images that it shouldn't.
$(document).ready(function() { $('div').not(":visible").each(function () { $(this).find('img').each(function() { $(this).attr("src",""); }); }); }); Is there a good javascript solution for this? Or do I have to use <img srctmp="...."/> and then replace srctmp by src via javascript for those images which are NOT nested within a hidden <div>?
srcattribute to""doesnt impact this. Try to remove the attribute whole after saving it todata-srcor something?srcattribute empty and then insert it later it with another attribute, but usedata-srctmpinstead.$(document).ready(function() {...});will always get executed after the DOM is ready (meaning after all the images and content has been loaded). So don't useready.readyyou can’t access any element. When images finish loading theloadevent is dispatched, notDOMContentLoaded(ready).