I have a project at work where we’re looking to present a user with an edit message if they’ve changed anything within a web page and haven’t saved the form before closing the page to stop them from losing changes. The following example works with IE 6 (which is our corporate browser). It does not seem to work with firefox, and I haven’t checked with any other browser since our need is isolated to IE.
var needToConfirm = false;
//Call this function if some changes is made to the web page and requires an alert
//you could call this is Keypress event of a text box or so…
needToConfirm = true;
//this could be called when save button is clicked
needToConfirm = false; //Call this function if dosent requires an alert.
window.onbeforeunload = confirmExit;
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?”;