Thanks to Matt Kruse (
http://www.JavascriptToolbox.comhttp:// www.AjaxToolbox.com) for providing the majority of the solution.
Ideally I would have liked to use the onunload event, or even the
onbeforeunload event but it seems from my research that is not
possible to get either of these events to cancel. Strange but
apparently true.
Matt very kindly provided an unpdate to his code to handle the command
buttons I had on my form
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Modify this part of the function:
for (i=0;i<theform.elements.length;i++) {
var changed=false;
var name=theform.elements[i].name;
if(!isBlank(name)){
var type=theform.elements[i].type;
if(!ignoreFields[name]){
if(type=="hidden"&&hiddenFields[name])
{changed=isChanged(theform[name]);}
else if(type=="hidden"){changed=false;}
else {changed=isChanged(theform[name]);}
}
}
if(changed){return true;}
}
return false;
}
to be this:
for (i=0;i<theform.elements.length;i++) {
var changed=false;
var name=theform.elements[i].name;
if(!isBlank(name)){
var type=theform.elements[i].type;
if(type!="submit" && type!="reset" && !ignoreFields[name]){ // THIS
LINE IS
CHANGED
if(type=="hidden"&&hiddenFields[name])
{changed=isChanged(theform[name]);}
else if(type=="hidden"){changed=false;}
else {changed=isChanged(theform[name]);}
}
}
if(changed){return true;}
}
return false;
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
and then for the really simple ones amongst us who have little idea
about javascript etc this is what I did:
1. Modified the file validations.js in the way described above.
2. I then added my own simple function to this file:
function confirmExit()
{
if (isFormModified(document.form2, null, null))
{
return (confirm('Are you sure you wish to lose your unsaved
changes?'))
}
return true;
}
3. I added this line into my ASP.net page between the </BODYand
<HEADtags:
<script src="validations.js"></script>
4. On each of my controls that could potentially cause data loss I
added this command (this is in ASP.net - in javascript I guess it
would be onclick)
OnClientClick="return confirm('Are you sure you want to delete
this record?');"
Thats it. It works. Hope that is helpful for someone else.
Jon Bosker
www.dbgurus.com.au