473,770 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"dispatchin g" 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=functi on(e){<initiali zes variable 1>}
....
method_n=functi on(e){<initiali zes 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=i nit_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(me thod_1);
....
init_all.add(me thod_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 1814

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=functi on(e){<initiali zes variable 1>}
...
method_n=functi on(e){<initiali zes 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=i nit_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.lengt h>0)
for(var i=0; i<arguments.len gth; 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/addEventListene r 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(hand ler) {
return function() {
handler.call(th is, arguments[0]);
func.call(this, arguments[0]);
}
})(obj[evt]);
} else {
obj[evt]=func;
}
}
addListener(win dow, "onload", function(){aler t("1")});
addListener(win dow, "onload", function(){aler t("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.j s>

Ex.:

---
<script type="text/javascript" src="eventListe nerLRN.js"></script>
<script type="text/javascript">
EventListener(w indow);
window.addEvent Listener("load" , function(){aler t("1")}, false);
window.addEvent Listener("load" , function(){aler t("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.j s>
OK that's the best, thanks !
--
Une Bévue
Mar 5 '07 #5
RobG <rg***@iinet.ne t.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************ *********@wanad oo.fr.invalidwr ote:
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
2608
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. I've read arguments, here and elsewhere, to the effect that in Python isinstance should be avoided like the plague, except in a few very specific and narrow circumstances. Roughly speaking, due in part to Python's dynamic nature its better to...
17
1597
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 probably reads or even writes to c.l.c++.m sometimes. Christopher gives this code (yes, it's not C++): interface IFuBar { contract: void FuBar();
2
26588
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 that key show in the textbox, or if other control has the focus and i press other key also show in the textbox. I saw some info about this using the user32.dll like this: static extern bool TranslateMessage( ref Message lpMsg);
206
8372
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 footprint which says: "Indeed, one often hears arguments against building exibility into an engineered sys- tem. For example, in the philosophy of the computer language Python it is claimed: \There should be one|and preferably only one|obvious...
6
1607
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 the client host address, and the other is the user name provided as for Basic Authentication (http://user@www.bla-bla.com). To do this, at the present I've overwritten the do_POST method of SimpleXMLRPCRequestHandler, including at a certain...
0
9453
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10099
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9904
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8929
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7451
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5481
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.