473,396 Members | 1,924 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,396 software developers and data experts.

Multiple Event Listeners for an Event: is there a way to execute them sequentially?

Frinavale
9,735 Expert Mod 8TB
Is there a way to order which event handler is handled first?
If I have 2 event handlers listening for the onbeforeunload event, is there a way for one event handler wait until the other event handler is finished?

-Frinny
Apr 6 '10 #1

✓ answered by gits

so in case it is not sure which handler is assigned first then how should the sequence be? ... the only way i could think of would be to let one handler set something and let the other wait with an interval ... if the 'set' didn't occur already then trigger the wished function first and then the next time the intentionally second handler would find the 'set' value ...

to the other question: you could ask if a node already has a handler assigned like:

Expand|Select|Wrap|Line Numbers
  1. var node = document.getElementById('foo');
  2.  
  3. if (typeof node.onclick == 'function') {
  4.     alert('node has a click-handler assigned');
  5. }
kind regards,
gits

9 3419
Dormilich
8,658 Expert Mod 8TB
I’m not exactly sure, but I think that the handlers are executed one after the other in order of their assignment (of course this doesn’t apply to IE)
Apr 6 '10 #2
gits
5,390 Expert Mod 4TB
as far as i'm aware it should be as Dormilich said ... but personally i wouldn't do such things ... i mean adding more then one handler in such a way ... just find it not 'defined' enough ... i would always prefer to chain them for myself ... so that i would have control of all that ... the way i always use is:

1. have a custom addListener(node, type, callback) method
2. this checks whether a specific type of listener is registered for the node already
3. then combines the callbacks in case one is present already

so i could always rely on the sequence of execution ...

kind regards
Apr 7 '10 #3
Frinavale
9,735 Expert Mod 8TB
just find it not 'defined' enough ... i would always prefer to chain them for myself ...
The content is dynamically generated and it could be difficult to keep track of which handler was assigned first.

this checks whether a specific type of listener is registered for the node already
I'm not entirely sure how to approach this suggestion.
Any links or general ideas on how to accomplish this?

Thanks!

-Frinny
Apr 7 '10 #4
gits
5,390 Expert Mod 4TB
so in case it is not sure which handler is assigned first then how should the sequence be? ... the only way i could think of would be to let one handler set something and let the other wait with an interval ... if the 'set' didn't occur already then trigger the wished function first and then the next time the intentionally second handler would find the 'set' value ...

to the other question: you could ask if a node already has a handler assigned like:

Expand|Select|Wrap|Line Numbers
  1. var node = document.getElementById('foo');
  2.  
  3. if (typeof node.onclick == 'function') {
  4.     alert('node has a click-handler assigned');
  5. }
kind regards,
gits
Apr 7 '10 #5
Frinavale
9,735 Expert Mod 8TB
I'm going to try this:
Expand|Select|Wrap|Line Numbers
  1. if (typeof node.onclick == 'function') {
  2.     // do stuff
  3.  }
The reason I'm asking this question is because I have 2 methods that handle the onbeforeunload event. One method checks for changes made to the page and informs the user that if they leave the data provided so far will be lost (there's quite a bit of data to be entered on the page). The other method blocks the content while the page is sending a request to the server with a partially transparent <div> over top of everything and a "processing" message on top of that.

So, what happens is that the user is given the chance to cancel the unload event...but if they do the transparent div and processing message blocking the page prevent the user from accessing the page again.

Either I need to be able to tell when the user has cancelled the page unload or I need to be able to prevent the div and processing message from being displayed when the user is being informed of potential loss of work.

I don't think it's possible to tell when the user cancelled the page unload event so I'm trying to implement the second.

I think your suggestion will work for me :)

Thanks again!

-Frinny
Apr 7 '10 #6
Frinavale
9,735 Expert Mod 8TB
Hmm, this apparently doesn't work:
Expand|Select|Wrap|Line Numbers
  1. if(typeof window.onbeforeunload == 'function'){
  2.   alert('onbeforeunload handler already assigned');
  3. }
Apr 7 '10 #7
gits
5,390 Expert Mod 4TB
it should ... here is a working example:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <script type="text/javascript">
  3. function foo() {
  4.     if (typeof window.onbeforeunload == 'function') {
  5.         alert('foo');
  6.     }
  7. }
  8.  
  9. window.onbeforeunload = function() {
  10. };
  11. </script>
  12. <body onload="foo();">
  13. </body>
  14. </html>
  15.  
could it be that the handler isn't added at this time?
Apr 7 '10 #8
Frinavale
9,735 Expert Mod 8TB
Thanks Gits,

I tried testing it in a place where the window.onbeforeunload had not been assigned to a function yet. I moved typeof check into the function that is called to display the div and processing message and it works as I expected it to.

Thanks again :)

-Frinny
Apr 7 '10 #9
gits
5,390 Expert Mod 4TB
glad to hear that you got it working :)

kind regards,
gits
Apr 7 '10 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

17
by: abs | last post by:
My element: <span onclick="alert('test')" id="mySpan">test</span> Let's say that I don't know what is in this span's onclick event. Is it possible to add another action to this element's onclick...
4
by: Ken Madden | last post by:
I want to create multiple fileSystemWatchers in a Windows Service to constantly watch multiple folders for file creations and then execute certain code based on these actions. The problem is that...
0
by: Kamilche | last post by:
''' event.py An event manager using publish/subscribe, and weakrefs. Any function can publish any event without registering it first, and any object can register interest in any event, even...
1
by: Marek Murin | last post by:
Hi all, I have created vb.net user control that has to be used by vb6 form. Everything goes well with putting the vb.net user control on the VB6 form until I want to receive any event from my...
0
by: Eniac | last post by:
Hello, I've started using Enterprise Library 2.0 recently and I've encountered a problem that seems to be ... well... undocumented :) Basically, when I set a Trace Listener (formatted event...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
0
by: nyc10011 | last post by:
Does anyone know the proper syntax to insert (in the AS3 code below) so that additional flv's play sequentially from the same FLVPlybk component? ? I am trying to figure out how to incorporate AS3...
4
by: raylopez99 | last post by:
Compound question: first, and this is not easy, if there's a way to detect multiple simultaneous key presses in C# let me know (in the below code, keys c and d being pressed simultaneously or...
0
by: Maric Michaud | last post by:
Le Thursday 28 August 2008 03:43:16 norseman, vous avez écrit : Disctionaries are hash tables with a unique key and constant time lookup. What you want could be implemented as a complex data...
0
by: Composer | last post by:
More experienced programmers may laugh that I had to struggle with this, but I post it so that others in my position might benefit. My application allows the user to have many forms open. Some...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.