473,785 Members | 2,129 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 1815

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
1598
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
26589
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
8374
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
1608
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
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9484
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
10350
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9957
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...
1
7505
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
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4055
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
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.