Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>One piece of the puzzle:</p> <pre><code>/** * Determines if a form is dirty by comparing the current value of each element * with its default value. * * @param {Form} form the form to be checked. * @return {Boolean} &lt;code&gt;true&lt;/code&gt; if the form is dirty, &lt;code&gt;false&lt;/code&gt; * otherwise. */ function formIsDirty(form) { for (var i = 0; i &lt; 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 &lt; element.options.length; j++) { if (element.options[j].selected != element.options[j].defaultSelected) { return true; } } } } return false; } </code></pre> <p><a href="http://developer.mozilla.org/en/DOM/window.onbeforeunload" rel="noreferrer">And another</a>:</p> <pre><code>window.onbeforeunload = function(e) { e = e || window.event; if (formIsDirty(document.forms["someFormOfInterest"])) { // For IE and Firefox if (e) { e.returnValue = "You have unsaved changes."; } // For Safari return "You have unsaved changes."; } }; </code></pre> <p>Wrap it all up, and what do you get?</p> <pre><code>var confirmExitIfModified = (function() { function formIsDirty(form) { // ...as above } return function(form, message) { window.onbeforeunload = function(e) { e = e || window.event; if (formIsDirty(document.forms[form])) { // For IE and Firefox if (e) { e.returnValue = message; } // For Safari return message; } }; }; })(); confirmExitIfModified("someForm", "You have unsaved changes."); </code></pre> <p>You'll probably also want to change the registration of the <code>beforeunload</code> event handler to use <code>LIBRARY_OF_CHOICE</code>'s event registration.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload