Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 16th, 2005, 10:16 PM
Authorized User
 
Join Date: Feb 2005
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to IronStar
Default 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
 
Old July 17th, 2005, 05:39 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)
 
Old July 17th, 2005, 12:33 PM
Authorized User
 
Join Date: Feb 2005
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to IronStar
Default

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
 
Old July 19th, 2005, 03:30 AM
Friend of Wrox
 
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

Cheers

 
Old July 19th, 2005, 05:29 AM
Authorized User
 
Join Date: Jul 2005
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to prophet Send a message via MSN to prophet
Default

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();">

 
Old July 19th, 2005, 05:30 AM
Authorized User
 
Join Date: Jul 2005
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to prophet Send a message via MSN to prophet
Default

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

 
Old July 19th, 2005, 09:18 PM
Authorized User
 
Join Date: Feb 2005
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to IronStar
Default

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?
 
Old July 20th, 2005, 02:13 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

You cannot stop them unloading the browser and to assign an event handler you need lowercase:
Code:
window.onunload = handleUnload;
--

Joe (Microsoft MVP - XML)
 
Old July 23rd, 2005, 07:00 PM
Authorized User
 
Join Date: Feb 2005
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to IronStar
Default

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?





Similar Threads
Thread Thread Starter Forum Replies Last Post
Preventing page refresh from asp.net button hericles ASP.NET 1.0 and 1.1 Professional 2 October 8th, 2008 04:29 PM
Preventing "Back", and duplicate responses shellhb ASP.NET 1.0 and 1.1 Basics 2 October 25th, 2005 07:53 AM
Back Button DARSIN General .NET 0 March 1st, 2005 08:00 AM
history.back or hitting the back button won't work lian_a Classic ASP Basics 4 July 29th, 2004 12:14 AM
Data loss during backup / restore 7.0 to 2000 mat41 SQL Server 2000 0 February 19th, 2004 05:50 PM





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