attached javascript does not work in safari.
this script displays a confirmation message before navigating away from the current page.
surprisingly this works for the first time in safari (but not during subsequent submit).
Scenario:
- user makes some changes and press submit button
- confirmation message displayed to user
- user decided to press cancel button to stay back on the same page
- but afterword user unable to invoke submit again.
- submit button does not work.
P.S: This code works perfectly with othere browser i.e. IE7/8, FireFox.
function checksave() {
if (formIsDirty(myForm)) {
return "The form has been changed...";
}
}
function formIsDirty(form) {
for (var i = 0; i < form.elements.length; i++) {
var element = form.elements[i];
var type = element.type;
if (type == "checkbox" || type == "radio") {
if (element.checked != element.defaultChecked) {
return true;
}
}
else if (type == "hidden" || type == "password" || type == "text" ||
type == "textarea") {
if (element.value != element.defaultValue) {
return true;
}
}
else if (type == "select-one" || type == "select-multiple") {
for (var j = 0; j < element.options.length; j++) {
if (element.options[j].selected !=
element.options[j].defaultSelected) {
return true;
}
}
}
}
return false;
}
<body OnBeforeUnload ="return checksave();">
<form id="myForm" >
<table border="1">
<tr> <td>Enter Text:</td><td><input id="text1" type="text" value=""/> <br/> </td></tr>
<tr> <td><input id="button1" type="submit" value="Submit1"/> <br/> </td> <td><input id="button2" type="submit" value="Submit2"/> <br/> </td></tr>
</table>
</form>
</body>