Skip to content

Instantly share code, notes, and snippets.

@gpassarelli
Created March 23, 2012 21:16
Show Gist options
  • Save gpassarelli/2175133 to your computer and use it in GitHub Desktop.
Save gpassarelli/2175133 to your computer and use it in GitHub Desktop.
JavaScript: Show massage before user leave the page
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