HighDots Forums  

Image onload handler - how to access the image object?

Javascript JavaScript language (comp.lang.javascript)


Discuss Image onload handler - how to access the image object? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Roger Shrubber
 
Posts: n/a

Default Image onload handler - how to access the image object? - 04-21-2004 , 04:57 AM






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!

Reply With Quote
  #2  
Old   
Richard Cornford
 
Posts: n/a

Default Re: Image onload handler - how to access the image object? - 04-21-2004 , 05:42 AM






Roger Shrubber wrote:
<snip>
Quote:
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.




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.