Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Delete onload event handler initiated at img tag level?

1,958 views
Skip to first unread message

Tuxedo

unread,
Apr 19, 2013, 11:37:44 AM4/19/13
to
Is there a way to reliably remove an onload event that has been initiated
at the img tag level?

For example, the following would repeat the replacement indefinitely as the
image is replacing itself with itself:

function replace(){
document.getElementById("photo").src="pic2.jpg";
// delete onload???
}
<img src="pic1.jpg" onload="replace()" id="photo">

Is there way to effectively remove the event while having the onload event
handler in the image tag as above so it only happens once?

Maybe something like:
delete document.getElementById("photo").onload; // ???

Many thanks for any advise.
Tuxedo

Martin Honnen

unread,
Apr 19, 2013, 11:50:02 AM4/19/13
to
Tuxedo wrote:

> For example, the following would repeat the replacement indefinitely as the
> image is replacing itself with itself:
>
> function replace(){
> document.getElementById("photo").src="pic2.jpg";
> // delete onload???
> }
> <img src="pic1.jpg" onload="replace()" id="photo">
>
> Is there way to effectively remove the event while having the onload event
> handler in the image tag as above so it only happens once?
>
> Maybe something like:
> delete document.getElementById("photo").onload; // ???

I would use

function replace(img, imgUrl) {
img.onload = null;
img.src = imgUrl;
}

<img src="pic1.jpg" onload="replace(this, 'pic2.jpg');" alt="...">


--


Tuxedo

unread,
Apr 19, 2013, 3:09:16 PM4/19/13
to
Thanks for this neat solution, which can work similarly with
getElementById() but not with 'this' keyword. Either way, no need for the
delete operator.

Tuxedo
0 new messages