Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

alert to save form changes on window close 1

Status
Not open for further replies.

crowell

Programmer
Jan 17, 2002
19
US
Is there a way to alert a user that they have not saved their changes on a form, if they attempt to close the page without saving?
I can use an onChange function to set a variable when any field is changed.
But what is the best way to trigger the alert?
(This form is within a frame, so the user can click on an index link to replace the form page, as well as closing the whole browser.)
I also don't want the user in a loop where they cannot close the form at all without saving, just in case they changed something by accident and do not want to save it.
I just need to alert them that they did not save.
Thanks for any suggestions.
 
here's one way, but i think it only works in IE due to using the "onbeforeunload" handler:
[tt]
<html>
<head>
<title></title>

<script language=&quot;javascript&quot;>
function init() {
e = document.forms[0].elements;
for (x = 0; x < e.length; x++)
e[x].onchange = doChange;
document.body.onbeforeunload = function(){
if (window.formChanged)
return &quot;You haven't saved your changes.&quot; +
&quot;\n - To save, click 'Cancel', then click the 'Save' button.&quot; +
&quot;\n - To leave without saving changes, click 'OK'.&quot;;
};
}

function doChange() {
window.formChanged = true;
}
</script>

</head>

<body onLoad=&quot;init();&quot;>
<form>
<textarea></textarea>
<p/>
<input type=&quot;button&quot; name=&quot;&quot; value=&quot;Save&quot; onclick=&quot;&quot; />
</form>
</body>
</html>

[/tt]
=========================================================
if (!succeed) try();
-jeff
 
Thanks jemminger!!!! This is exactly what I needed!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top