How to "tell" my message box to pop up only when user close the browser?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nic829
    New Member
    • Jul 2007
    • 4

    How to "tell" my message box to pop up only when user close the browser?

    I am trying to write a code such that when user close the browser or netvigate to other page, a message box will show up.

    Code:
        <SCRIPT>
        function closeIt()
          {
            event.returnValue = "Any unsaved data will be lost.";
          }
        </SCRIPT>
    
    <BODY onbeforeunload="closeIt()">
    but then every time something in my web postback or reload, the same message box popup. I only want this to happen when user close the browser or netvigate to other page, not when the current webpage reload. How could I solve this problem?
    Last edited by Dormilich; Apr 2 '09, 08:28 AM. Reason: added [code] tags
  • nic829
    New Member
    • Jul 2007
    • 4

    #2
    How to &quot;tell&quot ; my message box to pop up only when user close the browser?

    I wrote the following code such that when user close my webpage or netvigate to other page, a message box pop up:

    Code:
    <head>    
    <SCRIPT>
        function closeIt()
          {
            event.returnValue = "Any unsaved data will be lost.";
          }
        </SCRIPT> 
    </head>
    <BODY onBeforeUnload="closeIt()">

    but in my webpage there is some dropdown list, and everytime when the selected index of dropdown list changed, there is an auto post back. so everytime when i select something from the dropdown list, the message appears even i'm not leaving the page. Is there any ways that my message box will appear only when the user close the webpage or netvigate to somewhere else?
    Last edited by Dormilich; Apr 2 '09, 08:29 AM. Reason: added [code] tags

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Merged threads .

      Comment

      • iam_clint
        Recognized Expert Top Contributor
        • Jul 2006
        • 1207

        #4
        [CODE=javascript]
        <script>
        window.onbefore unload = function() {
        return 'By clicking ok, the data on this page will be lost.';
        }
        </script>
        [/CODE]

        Comment

        • khushbu shah
          New Member
          • Aug 2008
          • 8

          #5
          Alert on unsaved data

          For this pop up comes evey time, whether any chabges done on page, how to verify control state before showing pop up

          Comment

          • acoder
            Recognized Expert MVP
            • Nov 2006
            • 16032

            #6
            Use a variable initially set to false:
            Code:
            var anychanges = false;
            then whenever a change is made, set it to true. In the function, add this:
            Code:
            .. = function() {
                if (anychanges) return ...
            }

            Comment

            Working...