Created
March 23, 2012 21:16
-
-
Save gpassarelli/2175133 to your computer and use it in GitHub Desktop.
JavaScript: Show massage before user leave the page
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.onbeforeunload = confirmExit; | |
function confirmExit() | |
{ | |
if (needToConfirm) | |
{ | |
// check to see if any changes to the data entry fields have been made | |
for (var i = 0; i < values.length; i++) | |
{ | |
var elem = document.getElementById(ids[i]); | |
if (elem) | |
if ((elem.type == 'checkbox' || elem.type == 'radio') | |
&& values[i] != elem.checked) | |
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?"; | |
else if (!(elem.type == 'checkbox' || elem.type == 'radio') && | |
elem.value != values[i]) | |
return "You have attempted to leave this page. If you have made any changes to the fields without clicking the Save button, your changes will be lost. Are you sure you want to exit this page?"; | |
} | |
// no changes - return nothing | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment