Solved

onbeforeunload in condition

Posted on 2006-07-21
2
403 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 23

Accepted Solution

by:
apresto earned 500 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

696 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