Christian Winter wrote:
> Am 23.05.2013 10:19, schrieb Thomas 'PointedEars' Lahn:
>> Christian Winter wrote:
>>> You can store the old event handler in a local variable and
>>> create a closure around it which re-adds it in a temporary
>>> onload handler.
>>>
>>> function bla() {
>>> var oldhandler = document.getElementById('bla').onload;
>>> document.getElementById('bla').onload = function() {
>>> document.getElementById('bla').onload = oldhandler;
>>> }
>>> document.getElementById('bla').src = 'another.jpg';
>>> }
>>>
>>> See
http://jibbering.com/faq/notes/closures/ for details on
>>> closures in ECMAScript.
>>
>> You have not answered the question
>
> I didn't spoon-feed the absolute answer, I illustrated saving
> and restoring the original handler (which I consider the main
> problem). I might have been a bit misled by the example code
> that changes the src attribute only once, in that case your
> solutions with either a globally accessible property or an OO
> approach would be preferable.
In your example you save the old event-handler property value in the *local*
variable, then overwrite the event-handler property value with a function in
which the old event-handler property value is overwritten with the saved
value. Then you change value of the “src“ property, thereby overwriting the
old value with *itself*. And you do that by retrieving the DOM object
reference repeatedly when you could have used a variable or simply “this”.
And the OP asked about how to save the value in one function and restore it
in *another*.
>> and your code not only does not make any sense as-is
>
> Please don't start nit-picking.
There is no nit to pick: your code is complete nonsense.
>> it is potentially harmful.
>
> -v please.
See below.
>> The “onload” event handler property value of Image instances must be set
> > to “null” after the event occurred, in order to avoid infinite
^^^^^^^^
> > triggering in Opera (which is probably what the OP wisely did; now they
^^^^^^^^^^
> > want to change the image resource and do it all over again, probably
> > for a slideshow).
>
> I haven't stumbled over that behaviour in Opera yet, and Google is
> unwilling to provide me with more insight. Do you have a link handy?
Granted, the behavior is known to me for animated GIFs in Opera, but it
might occur in other cases, too. IIRC it was either Martin Honnen who
pointed it out in de.comp.lang.javascript or Lasse Reichstein Nielsen who
pointed it out here; and no, I do not have a link.