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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

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

    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
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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)

    Comment

    • gits
      Recognized Expert Moderator Expert
      • May 2007
      • 5388

      #3
      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(nod e, 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

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by Gits
        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.

        Originally posted by Gits
        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

        Comment

        • gits
          Recognized Expert Moderator Expert
          • May 2007
          • 5388

          #5
          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:

          Code:
          var node = document.getElementById('foo');
          
          if (typeof node.onclick == 'function') {
              alert('node has a click-handler assigned');
          }
          kind regards,
          gits

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            I'm going to try this:
            Code:
            if (typeof node.onclick == 'function') {
                // do stuff
             }
            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

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Hmm, this apparently doesn't work:
              Code:
              if(typeof window.onbeforeunload == 'function'){
                alert('onbeforeunload handler already assigned');
              }

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5388

                #8
                it should ... here is a working example:

                Code:
                <html>
                <script type="text/javascript">
                function foo() {
                    if (typeof window.onbeforeunload == 'function') {
                        alert('foo');
                    }
                }
                
                window.onbeforeunload = function() {
                };
                </script>
                <body onload="foo();">
                </body>
                </html>
                could it be that the handler isn't added at this time?

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Thanks Gits,

                  I tried testing it in a place where the window.onbefore unload 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

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5388

                    #10
                    glad to hear that you got it working :)

                    kind regards,
                    gits

                    Comment

                    Working...