Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #19903
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.fsmpi.rwth-aachen.de!noris.net!newsfeed.arcor.de!newsspool3.arcor-online.net!news.arcor.de.POSTED!not-for-mail |
|---|---|
| Content-Type | text/plain; charset="UTF-8" |
| Message-ID | <1761150.Rbm2vAZQEd@PointedEars.de> (permalink) |
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Reply-To | Thomas 'PointedEars' Lahn <usenet@PointedEars.de> |
| Organization | PointedEars Software (PES) |
| Date | Thu, 23 May 2013 10:19:07 +0200 |
| User-Agent | KNode/4.10.2 |
| Content-Transfer-Encoding | 8Bit |
| X-Face | %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v |
| Face | iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== |
| Subject | Re: Q: How to reset onload event handler that was set to null? |
| Newsgroups | comp.lang.javascript |
| References | <knja7u$3mm$1@news.albasani.net> <519dada2$0$6636$9b4e6d93@newsspool2.arcor-online.net> |
| MIME-Version | 1.0 |
| Lines | 160 |
| NNTP-Posting-Date | 23 May 2013 10:19:08 CEST |
| NNTP-Posting-Host | 1d57f4e8.newsspool4.arcor-online.net |
| X-Trace | DXC=^:n4=cNgJo7]E=H1Q9`7874IUK<Cl32<14Fo<]lROoR18kF<OcfhCO;Z60UQ4YM3<<DZm8W4\YJN<b@mF9jNikd5IQbgJMhLo070gn^\1;FJ57 |
| X-Complaints-To | usenet-abuse@arcor.de |
| Xref | csiph.com comp.lang.javascript:19903 |
Show key headers only | View raw
Christian Winter wrote:
> Am 22.05.2013 22:36, schrieb Tuxedo:
>> Is it possible to set the onload event handler of an image object back to
>> its default once it has been set to null?
>>
>> For example, in the beginning of a function, which was triggered by the
>> onload event, the onload event handler is set to null so that various
>> image replacements within the function body will not retrigger the
>> function.
>>
>> The onload event handler of the object is however needed again at a
>> different time for some other function. Eg.:
>>
>> function bla(){
>> document.getElementById('bla').onload=null;
>>
>> // some img replacement stuff that should not retrigger another onload
>> event document.getElementById('bla').src='another.jpg';
>>
>> document.getElementById('bla').onload ...
>> // how to recreate the onload event handler after?
>>
>> }
>>
>> <img id='bla' src=some.jpg" onload="bla();">
>>
>> Can the event handler be recreated after it's been set to null?
>
> 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, and your code not only does not make any
sense as-is, it is potentially harmful. 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).
The question was (in correct terminology) how to restore the event-handler
property value, the *first* event listener for an event on an object. The
correct and simple answer is that the property value is a reference to a
Function instance; you can and have to store that value first (here: in a
variable) –
var listener = foo.on…;
– then later restore it:
bar.on… = listener;
Closures would be involved here only insofar that the variable would have to
be declared in an execution context common to both functions in order to be
available in both:
var listener;
function foo ()
{
listener = …;
}
function bar ()
{
… = listener;
}
The alternatives would be a globally available property of a native object –
var myData = {};
function foo ()
{
myData.listener = …;
}
function bar ()
{
… = myData.listener;
}
– or a property of the object referred by the “this” value (which would be
useful for a slideshow object):
var slideshow = {
foo: function () {
this.listener = …;
}
bar: function () {
… = this.listener;
}
};
slideshow.foo();
…
slideshow.bar();
However, in the last two cases it is not necessary to store the event-
handler property value as the value can be available by user-defined
property in the first place – *then* understanding closures would be vital
indeed. For example:
var slideshow = {
resetListener: function () {
this.target.onload = null;
},
onload: function () {
this.resetListener();
// …
},
start: function (target) {
this.target = target;
this.next();
},
stop: function () {
this.resetListener();
window.clearTimeout(
// …
);
},
next: function () {
/* or with Function.prototype.bind() if possible */
var me = this;
this.target.onload = function () {
me.onload();
me = null;
};
window.setTimeout(
// …
);
}
};
slideshow.start(document.images["foo"]);
--
PointedEars
Twitter: @PointedEars2
Please do not Cc: me. / Bitte keine Kopien per E-Mail.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Q: How to reset onload event handler that was set to null? Tuxedo <tuxedo@mailinator.com> - 2013-05-22 22:36 +0200
Re: Q: How to reset onload event handler that was set to null? Tuxedo <tuxedo@mailinator.com> - 2013-05-22 23:32 +0200
Re: Q: How to reset onload event handler that was set to null? Tuxedo <tuxedo@mailinator.com> - 2013-05-23 00:12 +0200
Re: Q: How to reset onload event handler that was set to null? JJ <duh@nah.meh> - 2013-05-23 08:47 +0700
Re: Q: How to reset onload event handler that was set to null? Christian Winter <thepoet_nospam@arcor.de> - 2013-05-23 07:48 +0200
Re: Q: How to reset onload event handler that was set to null? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-23 10:19 +0200
Re: Q: How to reset onload event handler that was set to null? Christian Winter <thepoet_nospam@arcor.de> - 2013-05-23 18:06 +0200
Re: Q: How to reset onload event handler that was set to null? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-23 18:31 +0200
Re: Q: How to reset onload event handler that was set to null? sake mera <sakemera@gmail.com> - 2013-05-23 14:02 -0700
csiph-web