So, the
onbeforeunload action.
This doesn't seem to have too many options. It acts differently in the different browsers, and no, in Firefox 4 or later you cannot change in any way the browser's security confirmation box ("Note that in Firefox 4 and later the returned string is not displayed to the user." - https://developer.mozilla.org/en/DOM/window.onbeforeunload ). In Firefox, the confirmation box invariably says: "this page is asking you to confirm that you want to leave data you have entered may not be saved".
So, once the onbeforeunload action is set, one can just show the browser's default confirmation dialog box or add another (underline -
another) confirmation or other kind of dialog box. One can't remove or skip the browser's default confirmation box.
Here's my code:
global_form_changed = true;
window.onbeforeunload = function (e) {
e = e || window.event;
ret = global_form_changed?"You have not saved your contents. Are you sure you want to leave?":null;
// For IE and Firefox prior to version 4
if (e) {
e.returnValue = ret;
}
// For Safari
return ret;
};
I'm using the global
global_form_changed variable so that i can either ask the user to save the content or not ask anything, depending on whether the user has actually made any changes to the form or not.
I'm modifying my
global_form_changed like this:
On every input within the form, i'm setting the onchange attribute with this:
'
onchange="global_frm_edit_changed = true"
'
Of course there are various other ways to see if the form has actually changed or not.