Resetting 'onbeforeunload'

Last post 11-20-2008 8:40 AM by NC01. 7 replies.

Sort Posts:

  • Resetting 'onbeforeunload'

    11-19-2008, 2:22 AM
    • Member
      677 point Member
    • rmdw
    • Member since 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 876

    I've created this simple JavaScript function which I've added to an external Javascript file and now call from server-side code on most every page:

        function ToggleBeforeUnload(onOff)
        {
          if (onOff == true)
            window.onbeforeunload = UnloadPage;
          else
          {
            window.onbeforeunload = null;
          }
        }

    It *appears* that when onOff = false the latter statement is having no effect.  In other words, by calling "window.onbeforeunload = null" what I want to do is NOT have it call "UnloadPage" anymore.

    Is it possible that I'm using the wrong approach and there's another way to disable the "onbeforeunload" event?

    Robert

    Robert Werner
    Vancouver, BC
    Technical Blog
    Pocket Pollster
  • Re: Resetting 'onbeforeunload'

    11-19-2008, 2:34 AM
    • Contributor
      2,598 point Contributor
    • Sathesh_Pandian
    • Member since 07-23-2007, 3:55 PM
    • Chennai
    • Posts 441

    hi,

    please change the code as follows.

     if (window.addEventListener) {
                        window.addEventListener("onbeforeunload", Unloadpage, false);
                    } else if (window.attachEvent) {
                        window.attachEvent("onbeforeunload", Unloadpage);
                    } else {
                        window.onbeforeunload= Unloadpage;
                    }

     

    try this,it will work

    Got what you needed. Please mark as answer.
  • Re: Resetting 'onbeforeunload'

    11-19-2008, 10:43 AM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 7:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs

    That worked for me. How are you setting your "onOff" variable? Here is what I tried:

    <form id="Form1" method="post" runat="server">
     <input type="button" onclick="window.onbeforeunload = null;" value="Turn it off">
    </form>

    <script type="text/javascript">
    <!--
    function windowOnBeforeUnload()
    {
     return 'Are you sure?';
    }

    window.onbeforeunload = windowOnBeforeUnload;
    // -->
    </script>

    NC...

  • Re: Resetting 'onbeforeunload'

    11-19-2008, 3:57 PM
    • Member
      677 point Member
    • rmdw
    • Member since 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 876

    NC,

    That's the way I used to have things considered but since then I've built a much more sophisticated mechanism that automatically saves all specially marked Session variables to a special table in the DB.  It then restores them the next time the user logs in.  It's done with serialization.  It works really well but does require that JavaScript function to call the UnloadPage javascript function.

     

    Sathesh,

    Thank you for your code but I don't understand it.  Could you explain to me where in your code you are disabling the call to UnloadPage?  Put another way, once I've setup 'window.onbeforeunload' to call UnloadPage, how do I disable this?  I know how to do it in C# with server-side event handlers but I simply want to learn how to do it with JavaScript.

     

    Robert

    Robert Werner
    Vancouver, BC
    Technical Blog
    Pocket Pollster
  • Re: Resetting 'onbeforeunload'

    11-19-2008, 7:19 PM
    • All-Star
      20,624 point All-Star
    • A1ien51
    • Member since 05-06-2005, 2:46 PM
    • MD USA
    • Posts 3,788

    How about something as simple as: 

    var isEnabled = true;
    window.onbeforeunload = function (e) {
      if(isEnabled){
        var e = e || window.event;

        if (e) {
          e.returnValue = 'Any string';
        }
        else{
          return 'Any string';
        }
      }
    }

    Eric

  • Re: Resetting 'onbeforeunload'

    11-19-2008, 8:25 PM
    Answer
    • Member
      677 point Member
    • rmdw
    • Member since 03-14-2005, 11:03 PM
    • Vancouver, BC, Canada
    • Posts 876
    I'm not sure what that code does, Eric.  In any case, I did some further research and definitively determined that the way to turn off an event handler is to just set it to null.
    Robert Werner
    Vancouver, BC
    Technical Blog
    Pocket Pollster
  • Re: Resetting 'onbeforeunload'

    11-19-2008, 8:34 PM
    • All-Star
      20,624 point All-Star
    • A1ien51
    • Member since 05-06-2005, 2:46 PM
    • MD USA
    • Posts 3,788

     Did you try the code or just look at it?

    Run the code the way it is....than run the code with the boolean set to false.

    Eric

  • Re: Resetting 'onbeforeunload'

    11-20-2008, 8:40 AM
    • All-Star
      74,705 point All-Star
    • NC01
    • Member since 08-26-2005, 7:33 PM
    • Posts 13,868
    • TrustedFriends-MVPs

    rmdw:

    NC,

    That's the way I used to have things considered but since then I've built a much more sophisticated mechanism that automatically saves all specially marked Session variables to a special table in the DB.  It then restores them the next time the user logs in.  It's done with serialization.  It works really well but does require that JavaScript function to call the UnloadPage javascript function.


    Sathesh,

    Thank you for your code but I don't understand it.  Could you explain to me where in your code you are disabling the call to UnloadPage?  Put another way, once I've setup 'window.onbeforeunload' to call UnloadPage, how do I disable this?  I know how to do it in C# with server-side event handlers but I simply want to learn how to do it with JavaScript.

     

    Robert

    The code is simple enough. When you press the button, the onbeforeunload is set to null, and won't be called if you "X" close the page. Otherwise it will.

    NC...

     

Page 1 of 1 (8 items)