Hi everyone, I'm new here.
I have intermediate skills in JavaScript and I was looking for a VERY simple way to set a dirty flag on a page - completely client-side.
So I came up with the following script and would like to get some feedback on it. The form I would be using this for has textboxes, selects and checkboxes.
Known bugs:
initial version is IE only, have not tested it on FF.
...I would then check the hidden form field on page unLoad (there is a normal save button as well), and if the user is leaving the page with unsaved changes, prompt him/her to save. I am just curious what folks here think of the methodology.
Thanks!
I have intermediate skills in JavaScript and I was looking for a VERY simple way to set a dirty flag on a page - completely client-side.
So I came up with the following script and would like to get some feedback on it. The form I would be using this for has textboxes, selects and checkboxes.
Known bugs:
initial version is IE only, have not tested it on FF.
Code:
// functions to handle "dirty flag"
var dirtyString;
function initDirtyFlag()
{
dirtyString = compileString();
}
function compileString()
{
var localString='';
var theForm = document.getElementById("linenCustomization");
for (i=0; i<theForm.elements.length;i++)
{
control = theForm.elements[i];
if(control.type == 'checkbox')
localString = localString + control.checked + '/';
else
localString = localString + control.value + '/';
}
// alert(localString);
return localString;
}
//set a hidden field in the form
function checkDirtyFlag()
{
var localString = compileString();
var isDirty = (localString != dirtyString);
var theForm = document.getElementById("linenCustomization");
theForm.isDirty.value = isDirty;
setTimeout('checkDirtyFlag()', 1000);
}
</script>
....
...
<body onload="initDirtyFlag();setTimeout('checkDirtyFlag()', 2000);">
<form name="linenCustomization" style="width: 750px;" action="/loms/addCustomLinen.spring" method="post">
<input type="hidden" name="isDirty" id="isDirty" value="false" />
Thanks!