?
Solved

onbeforeunload in condition

Posted on 2006-07-21
2
Medium Priority
?
407 Views
Last Modified: 2008-03-06
I have the following command that ask the user if he is sure he wants to exit the page:
<body onbeforeunload="return ' '">
I want to have this only in case I have changes in the page that the user did not save.
How can I check it and how do I give a condition in this place in the < BODY>
Thankyou
Anat
0
Comment
Question by:ANAT2403
2 Comments
 
LVL 23

Accepted Solution

by:
apresto earned 2000 total points
ID: 17158017
when you say Save what do you mean?

If you mean a form then you could create a function to store the value of form fields when thje page was first loaded, then have them compare when they leave the page, if they are different you could show the error:  i.e

(paste this into a blank document and name it with a .html extension and run it)_

<html>

<head>
<script>
var oldVal = "";

function saveValues()
{
   oldVal = document.myForm.tester.value;
}

function checkChanges()
{
  if ( oldVal != document.myForm.tester.value )
   { return 'Are you sure you wish to exit dude?';}
}

</script>
</head>
<BODY onload="saveValues();" onbeforeunload="return checkChanges();">
<form name="myForm">
<input type="text" name="tester">
</form>

</body>

</html>
0
 

Author Comment

by:ANAT2403
ID: 17160422
That's very nice.
What is left is the problem of the refresh. I wrote you in the .NET forum. Can you help?
Thankyou
Anat
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question