| 1 |
// $Id: saveguard.js,v 1.1 2007/11/27 20:48:36 starbow Exp $
|
| 2 |
|
| 3 |
/*
|
| 4 |
* Called when there is unsaved data on the page
|
| 5 |
* to warn users leaving (or reloading) page that they would lose their changes.
|
| 6 |
* The onbeforeunload event works in FF, IE and Safari, but not in Opera (yet).
|
| 7 |
* The form of the dialog is unmodifiable:
|
| 8 |
*
|
| 9 |
* Are you sure you want to navigate away from this page?
|
| 10 |
* Your message here
|
| 11 |
* Press OK to continue, or Cancel to stay on the current page.
|
| 12 |
*/
|
| 13 |
(function ($) {
|
| 14 |
Drupal.markPageUnsaved = function(msg) {
|
| 15 |
if (!$('body').is('.has-unsaved-changes')) { // Only do it once.
|
| 16 |
$('body').addClass('has-unsaved-changes');
|
| 17 |
if (!msg) { // default message
|
| 18 |
msg = "If you continue your unsaved changes will be lost.";
|
| 19 |
}
|
| 20 |
window.onbeforeunload = function() {return msg;};
|
| 21 |
$(':submit, input:image').click(function() {
|
| 22 |
window.onbeforeunload = null; // Turn off the warning before submit triggers it.
|
| 23 |
});
|
| 24 |
}
|
| 25 |
};
|
| 26 |
|
| 27 |
$(document).ready(function(){
|
| 28 |
msg = Drupal.settings.saveguard.msg;
|
| 29 |
$('input, select').change(function() {Drupal.markPageUnsaved(msg)}); // checkboxes, radio buttons and selects
|
| 30 |
$('input, textarea').keypress(function() {Drupal.markPageUnsaved(msg)}); // textfields and textareas
|
| 31 |
});
|
| 32 |
|
| 33 |
})(jQuery);
|