This article describes how JS detects whether form data changes when it leaves or refreshes the page. Share to everyone for your reference, specific as follows:
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" | | | = = = "Password" | | | type = = "Text" | | | = = = "TextArea") {if Elem
Ent.value!= element.defaultvalue) {return true; ' Else if (type = = ' Select-one ' | | | | ' = = = ' Select-multiple ') {for (var j = 0; J < Element.options.len Gth
J + +) {if (element.options[j].selected!= element.options[j].defaultselected) {return true;
}}} return false;
} window.onbeforeunload = function (e) {e = e | | window.event; if (Formisdirty (document.forms["Someform"])) {//IE and Firefox if (e) {e.returnvalue = "Sorry, the page data has been modified, has not been saved, indeed Are you set to refresh or leave this page?
"; //Safari browser return "Sorry, the page data has been modified, has not been saved,Are you sure you want to refresh or leave this page?
";
}
};
More readers interested in JavaScript-related content can view this site: "JavaScript in the JSON Operation tips Summary", "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills summary", " JavaScript animation effects and tips Summary, "JavaScript Error and debugging skills Summary", "JavaScript data structure and algorithm skills summary", "JavaScript traversal algorithm and Skills summary" and "JavaScript Mathematical operation Usage Summary"
I hope this article will help you with JavaScript programming.