By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,264 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

"dispatching" window.onload to several methods

P: n/a

i'd like to intercept the window.onload event in order to distribute it,
as needed, to several methods.

example :

suppose i have several methods doing unlinked initialisations:

method_1=function(e){<initializes variable 1>}
....
method_n=function(e){<initializes variable n>}

and i want a mean to execute all the methods 1 to n when the event
window.onload arroses as i would have an overall method "init_all" :

window.onload=init_all;
function init_all(e){
method_1(e);
...
method_n(e)
}

how could i "register" all those methods to the overall one ?
because i don't know, in advance, the methods 1 to n.

i'd like to have something like :

init_all.add(method_1);
....
init_all.add(method_n);
aside from that is the "dummy" arg"e" usefull ?

because i've seen, following a typo of me, forgetting this arg let the
function works as usual ???
--
Une Bévue
Mar 5 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a

Une Bévue wrote:
i'd like to intercept the window.onload event in order to distribute it,
as needed, to several methods.
One trick is to register functions in a queue by storing them in an
array. Then when the load event occurs, loop over the array and
execute the functions.

Or you can just keep extending window.onload with more functions.
--
Rob

Mar 5 '07 #2

P: n/a
ASM
Une Bévue a écrit :
i'd like to intercept the window.onload event in order to distribute it,
as needed, to several methods.

example :

suppose i have several methods doing unlinked initialisations:

method_1=function(e){<initializes variable 1>}
...
method_n=function(e){<initializes variable n>}

and i want a mean to execute all the methods 1 to n when the event
window.onload arroses as i would have an overall method "init_all" :

window.onload=init_all;
function init_all(e){
method_1(e);
...
method_n(e)
}
as usual, not very clear for me :
do you expect on loading to use some methods among all of those defined
or do you want to cover all of them ?
If it is "all" ... don't see the difficulty.
how could i "register" all those methods to the overall one ?
because i don't know, in advance, the methods 1 to n.
Pareil : rien compris
I think window.onload wants to say "when.window.is.loaded.do ..."
so methods are known from this point, no?
aside from that is the "dummy" arg"e" usefull ?

because i've seen, following a typo of me, forgetting this arg let the
function works as usual ???
I think is is not usefull if it is not used :-)

Anyway without (e) you can have argument(s) when you call a function

<html>
<script type="text/javascript">
function hello() {
if(arguments && arguments.length>0)
for(var i=0; i<arguments.length; i++)
alert(arguments[i]);
}
</script>
<button onclick="hello('salut Yvon','comment va?');">hello</button>
</html>
--
Stephane Moriaux et son (moins) vieux Mac déjà dépassé
Mar 5 '07 #3

P: n/a
Une Bévue wrote:

Hi,
how could i "register" all those methods to the overall one ?
because i don't know, in advance, the methods 1 to n.
One way is to use attachEvent/addEventListener rather than the regular
<obj>.on<event>. Using these two, you can add as many methods as you
want. However, this does not go without problems, as non supporting
agents still require dedicated fallback. Moreover, IE's attachEvent does
not treat correctly the "this" value inside the handler, which may lead
to further issues.

Another basic way consists in simply enclosing the existing handler
within some new handler, using a closure, maybe adjusting the return
value in regards of your needs. The only problem with this approach is
some potential stack overflow if you add numerous listeners (say, many
thousands).

---
<script type="text/javascript">
function addListener(obj, evt, func) {
if(obj[evt]) {
obj[evt]=(function(handler) {
return function() {
handler.call(this, arguments[0]);
func.call(this, arguments[0]);
}
})(obj[evt]);
} else {
obj[evt]=func;
}
}
addListener(window, "onload", function(){alert("1")});
addListener(window, "onload", function(){alert("2")});
</script>
---

Eventually, it is probably better to define your own set of event
management methods, using a custom structure, with its own accessors
(add/remove and not only add), like RobG suggested.

Check out the following script by Lasse Reichstein Nielsen.

<URL:http://www.infimum.dk/privat/eventListener.js>

Ex.:

---
<script type="text/javascript" src="eventListenerLRN.js"></script>
<script type="text/javascript">
EventListener(window);
window.addEventListener("load", function(){alert("1")}, false);
window.addEventListener("load", function(){alert("2")}, false);
</script>
---
aside from that is the "dummy" arg"e" usefull ?
The 'e' argument refers to the event object, which in W3C's compliant
agents is passed as first argument; it may not be necessarily useful for
events such as 'load', however it is essential for events like 'click',
or 'keypress', in which you often have to retrieve event properties to
work some decent effect.
Kind regards,
Elegie.
Mar 5 '07 #4

P: n/a
Elegie <el****@invalid.comwrote:
Eventually, it is probably better to define your own set of event
management methods, using a custom structure, with its own accessors
(add/remove and not only add), like RobG suggested.

Check out the following script by Lasse Reichstein Nielsen.

<URL:http://www.infimum.dk/privat/eventListener.js>
OK that's the best, thanks !
--
Une Bévue
Mar 5 '07 #5

P: n/a
RobG <rg***@iinet.net.auwrote:
>
One trick is to register functions in a queue by storing them in an
array. Then when the load event occurs, loop over the array and
execute the functions.
thanks adopted !
--
Une Bévue
Mar 5 '07 #6

P: n/a
ASM <st*********************@wanadoo.fr.invalidwrote :
I think window.onload wants to say "when.window.is.loaded.do ..."
so methods are known from this point, no?
No because i want to put that feature in a toolkit, i'll use the
suggestions from Rob & Elegie.

for the other "prob", i do have to verify but i think, one time,
forgetting the callback arg e i was able to use properties of it.

"as if" e was added automatically because the function relied on an
event ????
--
Une Bévue
Mar 5 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.