 |
| 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
|
|
|
|

July 16th, 2005, 10:16 PM
|
|
Authorized User
|
|
Join Date: Feb 2005
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 17th, 2005, 05:39 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
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)
|
|

July 17th, 2005, 12:33 PM
|
|
Authorized User
|
|
Join Date: Feb 2005
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

July 19th, 2005, 03:30 AM
|
|
Friend of Wrox
|
|
Join Date: May 2004
Posts: 212
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Couldn't you use a cookie to grab the form data? I assume you are talking about form data?
Cheers
|
|

July 19th, 2005, 05:29 AM
|
|
Authorized User
|
|
Join Date: Jul 2005
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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();">
|
|

July 19th, 2005, 05:30 AM
|
|
Authorized User
|
|
Join Date: Jul 2005
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
|
|

July 19th, 2005, 09:18 PM
|
|
Authorized User
|
|
Join Date: Feb 2005
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|
|

July 20th, 2005, 02:13 AM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
|
|
You cannot stop them unloading the browser and to assign an event handler you need lowercase:
Code:
window.onunload = handleUnload;
--
Joe ( Microsoft MVP - XML)
|
|

July 23rd, 2005, 07:00 PM
|
|
Authorized User
|
|
Join Date: Feb 2005
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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?
|
|
 |