p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   Javascript How-To (http://p2p.wrox.com/forumdisplay.php?f=87)
-   -   Preventing Data Loss when hitting back button (http://p2p.wrox.com/showthread.php?t=31553)

IronStar July 16th, 2005 10:16 PM

Preventing Data Loss when hitting back button
 
Hi,

I have a form where a user could have entered/updated some data. I can tell if any of the inputs have changed by creating a global boolean variable in the script tag and have the onChange() event handler set it to true if anything changes. If the user hits the back button or closes the browser by accident they will loose their updates. I can trap the event with onUnload and confirm that they are going to loose their updates. Can you stop the action of hitting the back button or closing the browser if the user does not want to loose their updates? Or can you temporarily disable the browser's back and close buttons while they are updating data?

Thanks,
Tom

joefawcett July 17th, 2005 05:39 AM

In IE, not sure about other browsers, you can use onbeforeunload which allows the user to be warned that they will lose data at which point they can cancel the unload.

--

Joe (Microsoft MVP - XML)

IronStar July 17th, 2005 12:33 PM

Hi Joe,

Thanks for the advice. Is there a way to stop the page from unloading if the user accidentally closes the browser or hits the back button?

Tom

interrupt July 19th, 2005 03:30 AM

Couldn't you use a cookie to grab the form data? I assume you are talking about form data?

Cheers


prophet July 19th, 2005 05:29 AM

In Internet Explorer the onBeforeUnload event allows you to prompt the user if the browser is about to be closed, or the page is being navigated elsewhere.

Example:

<script language="javascript">
function handleOnBeforeUnload() {
  event.returnValue = "Some custom message in here.";
}
</script>

...

<body onBeforeUnload="handleOnBeforeUnload();">


prophet July 19th, 2005 05:30 AM

See also: http://msdn.microsoft.com/library/de...foreunload.asp


IronStar July 19th, 2005 09:18 PM

I am using Firefox as a browser. As a rule I try to stay as browser independent as possible.

I am experimenting with the captureEvents method as a way to override the default beahvior for the onUnLoad event:

      <script language="JavaScript1.2">
         var changed = false

         // override the document.unLoad event handler
         window.captureEvents(Event.UNLOAD);

         // Register the event handlers here:
         window.onUnLoad = handleUnload;

         // function to handle the unLoad
         function handleUnLoad
         {
            alert("The page has unloaded.");
         }

         // Function to set the changed flag to T
         function dataChanged()
         {
            changed = true
         }
      </script>

I am having some trouble getting it to work properly in IE and Firefox. Does anyone have any experience with this?

joefawcett July 20th, 2005 02:13 AM

You cannot stop them unloading the browser and to assign an event handler you need lowercase:
Code:

window.onunload = handleUnload;
--

Joe (Microsoft MVP - XML)

IronStar July 23rd, 2005 07:00 PM

It seems odd to me that the captureEvents method can accept Event.UNLOAD as an argument but cannot capture the event. Is this behavior documented anywhere?


All times are GMT -4. The time now is 03:49 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.