"dispatching" window.onload to several methods

U

Une Bévue

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 ???
 
R

RobG

Une said:
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.
 
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);
}
</script>
<button onclick="hello('salut Yvon','comment va?');">hello</button>
</html>
 
E

Elegie

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);
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.
 
U

Une Bévue

RobG said:
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 !
 
U

Une Bévue

ASM said:
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 ????
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top