S
Stanley
Hello all,
I have a need to make sure that no users leave a form without being warned
that they have un-saved data. I have the script below that I found on
egghead.com but there is an issue with it that if I have a dropdown that
posts back so that I can show another field if the user chooses other then
the warning pops up. If I use an attribute on the dropdown to use the
OnChange event and set the bSubmitted=True that takes care of the post back
issue but causes another issue. The other issue is now the user can leave
the form without any warning. So what I am looking for is a better way to be
able to handle form post backs and still give the user the warning. Has
anyone done this before?
TIA
-Stanley
[script]
var bSubmitted=false;
function isDirty(oForm)
{
if(bSubmitted)
{
return false;
}
var iNumElems = oForm.elements.length;
for (var i=0;i<iNumElems;i++)
{
var oElem = oForm.elements;
if ("text" == oElem.type || "TEXTAREA" == oElem.tagName)
{
if (oElem.value != oElem.defaultValue)
{
return true;
}
}
else if ("checkbox" == oElem.type || "radio" == oElem.type)
{
if (oElem.checked != oElem.defaultChecked)
{
return true;
}
}
else if ("SELECT" == oElem.tagName)
{
var oOptions = oElem.options;
var iNumOpts = oOptions.length;
for (var j=0;j<iNumOpts;j++)
{
var oOpt = oOptions[j];
if (oOpt.selected != oOpt.defaultSelected)
{
return true;
}
}
}
}
return false;
}
function checkFormStatus(){
var frm = document.forms[0];
if(isDirty(frm))
event.returnValue = "You have entered form Data, or Data has been entered
for you, without submitting this form.";
}
if ( typeof( window.addEventListener ) != "undefined" ) {
window.addEventListener("onbeforeunload", checkFormStatus, false);
} else if ( typeof( window.attachEvent ) != "undefined" ) {
window.attachEvent("onbeforeunload", checkFormStatus);
}
[/script]
I have a need to make sure that no users leave a form without being warned
that they have un-saved data. I have the script below that I found on
egghead.com but there is an issue with it that if I have a dropdown that
posts back so that I can show another field if the user chooses other then
the warning pops up. If I use an attribute on the dropdown to use the
OnChange event and set the bSubmitted=True that takes care of the post back
issue but causes another issue. The other issue is now the user can leave
the form without any warning. So what I am looking for is a better way to be
able to handle form post backs and still give the user the warning. Has
anyone done this before?
TIA
-Stanley
[script]
var bSubmitted=false;
function isDirty(oForm)
{
if(bSubmitted)
{
return false;
}
var iNumElems = oForm.elements.length;
for (var i=0;i<iNumElems;i++)
{
var oElem = oForm.elements;
if ("text" == oElem.type || "TEXTAREA" == oElem.tagName)
{
if (oElem.value != oElem.defaultValue)
{
return true;
}
}
else if ("checkbox" == oElem.type || "radio" == oElem.type)
{
if (oElem.checked != oElem.defaultChecked)
{
return true;
}
}
else if ("SELECT" == oElem.tagName)
{
var oOptions = oElem.options;
var iNumOpts = oOptions.length;
for (var j=0;j<iNumOpts;j++)
{
var oOpt = oOptions[j];
if (oOpt.selected != oOpt.defaultSelected)
{
return true;
}
}
}
}
return false;
}
function checkFormStatus(){
var frm = document.forms[0];
if(isDirty(frm))
event.returnValue = "You have entered form Data, or Data has been entered
for you, without submitting this form.";
}
if ( typeof( window.addEventListener ) != "undefined" ) {
window.addEventListener("onbeforeunload", checkFormStatus, false);
} else if ( typeof( window.attachEvent ) != "undefined" ) {
window.attachEvent("onbeforeunload", checkFormStatus);
}
[/script]