I was wondering, is there a way to detect if a certain image / div is loaded? For example when i am loading two heavy images and showing a loading sign at the two places the images will later occupy, is there a way to already display the first image when it's loaded while still loading the second one?
Asked
Active
Viewed 504 times
2
-
When the image is done 'loading' and ready to be displayed – Samuel May 24 '10 at 11:36
3 Answers
1
If you are using new Image
to preload images, then you can do the following to be notified of then it is loaded
var img = new Image();
img.onload = function() {
//display the image
document.getElementById("myDiv").innerHTML = "%3Cimg src='myimg.jpg' alt=''/%3E";
};
img.src = "myimg.jpg";
Remember to set the src
after the onload
.

Sean Kinsey
- 37,689
- 7
- 52
- 71
1
myImage.addEventListener('load', function() { ... }, false);
Code inside the above function will be called when the image is finished loading.

Delan Azabani
- 79,602
- 28
- 170
- 210
-
1True - as long as it hasn't finished loading BEFORE this line of JavaScript executes. – Fenton May 24 '10 at 10:35