Changing <img src="XXX" />, js event when new image has finished loading?

Posted by carillonator on Stack Overflow See other posts from Stack Overflow or by carillonator
Published on 2010-03-25T20:23:48Z Indexed on 2010/03/25 20:33 UTC
Read the original article Hit count: 277

I have a photo gallery web page where a single <img src="XXX" /> element's src is changed (on a click) with javascript to show the next image—a poor man's ajax I guess. Works great on faster connections when the new image appears almost immediately. Even if it takes a few seconds to load, every browser I've tested it on keeps the old image in place until the new one is completely loaded.

It's a little confusing waiting those few seconds on a slow connection, though, and I'm wondering if there's some javascript event that fires when the new image is done loading, allowing me to put a little working... animated gif or something up in the meantime.

I know I could use AJAX for real (I'm using jQuery already), but this is such a nice and simple solution. Besides this lag, is there any other reason I should stay away from this approach to changing images?

thanks.

© Stack Overflow or respective owner

Related posts about javascript-events

Related posts about JavaScript