By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 769 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Check form before unload for changes

P: n/a
Hi,

I have a javascript that checks a form for changes, if it has any it
alerts the user before they navigate away from the page.

it can be disabled on a submit button click by adding

onclick="javascript:bSubmitted=true;"

to it's tag.

However i have a checkbox that has AutoPostBack=true and would like to
disable it for that too, but if i add

onCheckChanged="javascript:bSubmitted=true;"

I get a compilation error when the page loads

CS1026: ) expected

Can anyone suggest a fix? The javascript is below, just incase it's
useful.

Thanks

function checkFormStatus(oForm){
if(isDirty(oForm))
event.returnValue = "You have entered form Data without submitting this
form.";
}

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[i];

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;
}

Nov 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
onCheckChanged is a server-side event handler. You need this to add a client
side handler:

myCheckBox.Attributes["onclick"] = "javascript:bSubmitted=true;";

Eliyahu

"Assimalyst" <c_******@hotmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Hi,

I have a javascript that checks a form for changes, if it has any it
alerts the user before they navigate away from the page.

it can be disabled on a submit button click by adding

onclick="javascript:bSubmitted=true;"

to it's tag.

However i have a checkbox that has AutoPostBack=true and would like to
disable it for that too, but if i add

onCheckChanged="javascript:bSubmitted=true;"

I get a compilation error when the page loads

CS1026: ) expected

Can anyone suggest a fix? The javascript is below, just incase it's
useful.

Thanks

function checkFormStatus(oForm){
if(isDirty(oForm))
event.returnValue = "You have entered form Data without submitting this
form.";
}

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[i];

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;
}

Nov 19 '05 #2

P: n/a
Thanks for the suggestion, but i'm pretty new to all this.

Could you explain in a little more detail how to do this? I can't get
it to work.

Thanks again.

Nov 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.