Skip to content

Instantly share code, notes, and snippets.

@yaci
Created August 17, 2017 22:55
Show Gist options
  • Save yaci/29bb5beada847d595255ceecacddb6dc to your computer and use it in GitHub Desktop.
Save yaci/29bb5beada847d595255ceecacddb6dc to your computer and use it in GitHub Desktop.
Check if all images on page are loaded using JavaScript
//WARNING! May not work with some SVG images
//For some SVGs the naturalWidth/naturalHeight attributes will equal 0
//even if image is properly loaded
var allLoaded=true;
var images = document.images;
// or: var images = document.getElementsByTagName('img');
for (i=0; (i<images.length) && allLoaded; i++) {
result = images[i].complete && (typeof images[i].naturalWidth!="undefined" && images[i].naturalWidth>0);
allLoaded=allLoaded && result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment