I am looking for a way to implement this for as many browsers as possible:
var image = new Image();
image.addEventListener("load", function() {
alert("loaded");
}, false);
image.src = "image_url.jpg";
This didn’t work in IE so I googled and found out that IE’s lower than 9 do not support .addEventListener(). I know there is a jQuery equivalent called .bind() but that doesn’t work on an Image(). What do I have to change for this to work in IE too?
IE supports attachEvent instead.
With jQuery, you can just write
When it comes to events, if you have the possibility of using jQuery I would suggest using it because it will take care of browser compatibility. Your code will work on all major browser and you don’t have to worry about that.
Note also that in order to the load event being fired in IE, you need to make sure the image won’t be loaded from the cache, otherwise IE won’t fire the load event.
To do this, you can append a dummy variable at the end of the url of your image, like this
Some known issues using the jQuery load method are
See the jQuery docs for more info.