469,610 Members | 1,554 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,610 developers. It's quick & easy.

Image onload handler - how to access the image object?

Good day

In an image onload handler, how do I access the image that just loaded?

If the image is attached to the document, I can access it using the field
window.event.srcElement. But I really don't want to attach the image to the
document until it has loaded (I plan to use it to replace an existing visible
low-res version of the same image). The onload handler is called for the
unattached image, but the window.event.srcElement is NULL.

I'm using IE 6.0, but I need a cross-browser solution.

Cheers!
Jul 23 '05 #1
1 2662
Roger Shrubber wrote:
<snip>
If the image is attached to the document, I can access it using the
field window.event.srcElement. But I really don't want to attach the
image to the document until it has loaded (I plan to use it to
replace an existing visible low-res version of the same image). The
onload handler is called for the unattached image, but the
window.event.srcElement is NULL.

I'm using IE 6.0, but I need a cross-browser solution.


Event handling functions are methods of objects (and are called as
such), in all methods (called as methods) the - this - keyword refers to
the object. So:-

var img = new Image();
img.onload = function(){
document.images['imgName'].src = this.src;
}
img.src = "http://example.com/image.gif";

Richard.
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by TheKeith | last post: by
4 posts views Thread by Pai | last post: by
2 posts views Thread by inevercheckthisaddress | last post: by
2 posts views Thread by laredotornado | last post: by
3 posts views Thread by jon | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.