PDA

View Full Version : warn a user to save information before skipping to other links(on the same screen or


bindu
09-05-2002, 05:34 PM
how can i prevent a user from moving to another link(on the same screen or on different frame in the frameset) with out getting confirmation whether he wants to save the information that he entered (changed) on the current screen.
i just want to alert the user with a (confirm) message that asks if he would like to save the changes that he made. if he clicks "NO", he will be taken to the screen that he requested(to the link that he clicked). otherwise remain on the current screen so that he could hit the 'save changes' button on that screen.

ShriekForth
09-05-2002, 05:50 PM
This is how I tried to solve that problem.
It seemed to work fine in most browsers, there was a bug in some version of IE 5.01 or soemthing that would actually fetch the page they were trying to go to as the dialog was up, so the information was lost. Initially I thought of doing a javascript wrapper around all my links, but that does not stop them navigating away from the site. After a few days of trying different ways, I came up with this.


<script language="JavaScript">
var changes = false;
var nsFix = false;
function setWarn(){
changes = true;
}

function Warn(){
if ((changes == true)&&(nsFix == false)){
if(confirm("Changes have been made on this profile page.
\nClick OK to save the changes to the profile.\n
Click Cancel to ignore the changes and return to the profile page.")){
nsFix=true;
return saveForm();
}
else{
nsFix=true;
return true;
}
}
else{
return true;
}
}
function formReset(){
changes = false;
nsFix = true;
}

</script>


Then the body will need onUnload="return Warn();"
and each form element will need onChange="setWarn()";
Your submit button or script will need to reset changes to false so that the warning does not appear when the user selects submit.
I don't remember what the nsFix fixed, you could remove it an see, I think it kept popping the dialog up over and over.
ShriekForth

beetle
09-05-2002, 05:52 PM
function confirmSave() {
return !(confirm("You are about to continue without saving. Do you wish to save first?"));
}
<a href="page.htm" onClick="return confirmSave();">link</a>