473,406 Members | 2,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

"dispatching" window.onload to several methods


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
6 1770

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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Jordan Rastrick | last post by:
Hi everyone, Just a little issue that I've come across in Python where I'd be interested to read the thoughts and opinions of the great thinkers and programmers who frequent this newsgroup. ...
17
by: Victor Bazarov | last post by:
Interesting article. However, it starts with an example which I find rather misleading. I hope the author (Christopher Diggins) wouldn't mind if I post a quote from the article. I know he...
2
by: Juan Martinez | last post by:
In a form i have a drawing control called VectorDraw and a textbox. The text box is disable, when i am using the drawing area i can write some text, what i want to do is that when i press any key...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
6
by: Luigi | last post by:
Dear all, I'm writing an XML-RPC server which should be able to modify the incoming request before dispatching it. In particular I wand to added two fixed parameters to the method called: one is...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.