I'm trying to write a simple script - a page is composed of a number of checkboxes. If a user clicks any of them, it changes a variable in JS that flags that there has been a change in the selection of checkboxes. When the user navigates away, I want it to check to see if a change has been made and, if so, ask the user to confirm that they want to leave. My code is as follows:
I've implemented it using <body onunload="return changesMade()">; however, when the user clicks "Cancel", it still navigates away from the page. I've tried implementing it with onbeforeunload="return changesMade()", but Firefox and IE implement it with their own annoying "are you sure you want to navigate away?" pop-ups. Any idea on how to stop the browser from navigating away from the page when the user clicks "Cancel"?
Code:
// This will ask the user, if changes have been made, if they're sure they want to navigate away function changesMade() { if (isChanged == 1) { if (confirm('You have made changes to your selection here. Are you sure you wish to navigate away from this page?')) { return true; } else { return false; } } return true; }
I've implemented it using <body onunload="return changesMade()">; however, when the user clicks "Cancel", it still navigates away from the page. I've tried implementing it with onbeforeunload="return changesMade()", but Firefox and IE implement it with their own annoying "are you sure you want to navigate away?" pop-ups. Any idea on how to stop the browser from navigating away from the page when the user clicks "Cancel"?
the idiot is the person who follows the idiot and your not following me your insulting me your following the path of a idiot so that makes you the idiot - LC Tusken