7 Replies Latest reply on May 7, 2009 2:09 PM by dmcghan

    How to add a onbeforeunload event?

    Alistair Laing
      Hi

      I know APEX has a built JS function called addLoadEvent() which basically appends another js function to an existing onload event which maybe created from the template.

      My question is whether there is a function that appends js functions to an existing onBeforeUnload event? If not how could I go about it? I would think that I would need to somehow search the text between the < and > of the body tag for any onbeforeunload and if there was already on then just to add it to the end.

      In my template I have defined an onBeforeUnload event within the body tag which works perfectly however if you have another page that has a onBeforeUnload event in the HTML region it just simply ignores the pages HTML regions onBeforeUnload event.

      Just need some help with this.
        • 1. Re: How to add a onbeforeunload event?
          dmcghan
          Alistair,

          Why not bind it to a single function and call your various functionality from within?

          Regards,
          Dan

          http://danielmcghan.us
          http://sourceforge.net/projects/tapigen
          http://sourceforge.net/projects/plrecur
          • 2. Re: How to add a onbeforeunload event?
            Alistair Laing
            Hi Dan

            Could you explain?

            I managed to get hold of the addLoadEvent function in APEX and created my own addBeforeonload Event which seems to work in FF with no JS errors but in IE it works and runs the second onbeforeunload even but then throws a JS error.
            function addUnLoadEvent(pFunction) {
              var oldonbeforeunload = window.onbeforeunload;
              if (typeof window.onbeforeunload != 'function') {
                window.onbeforeunload = pFunction;
              } else {
                window.onbeforeunload = function() {
                  if (oldonbeforeunload) {oldonbeforeunload();}
                  pFunction();
                };
              }
            }
            • 3. Re: How to add a onbeforeunload event?
              dmcghan
              Alistair,

              Did you add both of the onbeforeunload eventsfrom your first question?

              Regards,
              Dan

              http://danielmcghan.us
              http://sourceforge.net/projects/tapigen
              http://sourceforge.net/projects/plrecur
              • 4. Re: How to add a onbeforeunload event?
                Alistair Laing
                Hi Dan,

                In my Template under the Header seciont I have the following:
                /**
                * Adds a function to the window onunload event
                */
                function addUnLoadEvent(pFunction) {
                  var oldonbeforeunload = window.onbeforeunload;
                  if (typeof window.onbeforeunload != 'function') {
                    window.onbeforeunload = pFunction;
                  } else {
                    window.onbeforeunload = function() {
                      if (oldonbeforeunload) {oldonbeforeunload();}
                      pFunction();
                    };
                  }
                }
                addUnLoadEvent(html_Submit_Progress());
                </script>#HEAD#</head>
                <body #ONLOAD#>#FORM_OPEN#
                Then on my page I have the following in the page header:
                <script>
                function test(){
                alert("hello");
                }
                addUnLoadEvent(test());
                </script>
                The html_Submit_Progress fires and so does the test() but in IE it throws an error after displaying the alert.
                • 5. Re: How to add a onbeforeunload event?
                  dmcghan
                  Alistair,

                  Perfect... Rather than try to bind two separate functions to the event, create one function that calls the two and call that function instead:
                  function beforeLeaving() {
                     html_Submit_Progress();
                     test();
                  }
                  Regards,
                  Dan

                  http://danielmcghan.us
                  http://sourceforge.net/projects/tapigen
                  http://sourceforge.net/projects/plrecur
                  • 6. Re: How to add a onbeforeunload event?
                    Alistair Laing
                    But can you add/remove functions within the beforeleaving function? It just to ensure that if a page does have a onbeforeunload event it would add it to the existing onbeforeunload event.
                    • 7. Re: How to add a onbeforeunload event?
                      dmcghan
                      Alistair,

                      You could use conditional logic within to call functions or not.

                      Regards,
                      Dan

                      http://danielmcghan.us
                      http://sourceforge.net/projects/tapigen
                      http://sourceforge.net/projects/plrecur