Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old July 16th, 2005, 11:16 PM
Authorized User
 
Join Date: Feb 2005
Location: Smithtown, NY, USA.
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
Reply With Quote
  #2 (permalink)  
Old July 17th, 2005, 06:39 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #3 (permalink)  
Old July 17th, 2005, 01:33 PM
Authorized User
 
Join Date: Feb 2005
Location: Smithtown, NY, USA.
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
Reply With Quote
  #4 (permalink)  
Old July 19th, 2005, 04:30 AM
Friend of Wrox
 
Join Date: May 2004
Location: , , .
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

Reply With Quote
  #5 (permalink)  
Old July 19th, 2005, 06:29 AM
Authorized User
 
Join Date: Jul 2005
Location: Westchester County, NY, USA.
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();">

Reply With Quote
  #6 (permalink)  
Old July 19th, 2005, 06:30 AM
Authorized User
 
Join Date: Jul 2005
Location: Westchester County, NY, USA.
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

Reply With Quote
  #7 (permalink)  
Old July 19th, 2005, 10:18 PM
Authorized User
 
Join Date: Feb 2005
Location: Smithtown, NY, USA.
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?
Reply With Quote
  #8 (permalink)  
Old July 20th, 2005, 03:13 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
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)
Reply With Quote
  #9 (permalink)  
Old July 23rd, 2005, 08:00 PM
Authorized User
 
Join Date: Feb 2005
Location: Smithtown, NY, USA.
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?
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 05:29 PM
Preventing "Back", and duplicate responses shellhb ASP.NET 1.0 and 1.1 Basics 2 October 25th, 2005 08: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 01:14 AM
Data loss during backup / restore 7.0 to 2000 mat41 SQL Server 2000 0 February 19th, 2004 05:50 PM



All times are GMT -4. The time now is 10:20 AM.


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