Image onload handler - how to access the image object?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Roger Shrubber

    #1

    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.sr cElement. 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.sr cElement is NULL.

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

    Cheers!
  • Richard Cornford

    #2
    Re: Image onload handler - how to access the image object?

    Roger Shrubber wrote:
    <snip>[color=blue]
    > If the image is attached to the document, I can access it using the
    > field window.event.sr cElement. 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.sr cElement is NULL.
    >
    > I'm using IE 6.0, but I need a cross-browser solution.[/color]

    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.


    Comment

    Working...