summaryrefslogtreecommitdiff
path: root/extensions/Vector/modules/ext.vector.editWarning.js
diff options
context:
space:
mode:
Diffstat (limited to 'extensions/Vector/modules/ext.vector.editWarning.js')
-rw-r--r--extensions/Vector/modules/ext.vector.editWarning.js61
1 files changed, 23 insertions, 38 deletions
diff --git a/extensions/Vector/modules/ext.vector.editWarning.js b/extensions/Vector/modules/ext.vector.editWarning.js
index e6ee5c60..e128fd45 100644
--- a/extensions/Vector/modules/ext.vector.editWarning.js
+++ b/extensions/Vector/modules/ext.vector.editWarning.js
@@ -11,22 +11,11 @@
$( '#wpTextbox1, #wpSummary' ).each( function () {
$(this).data( 'origtext', $(this).val() );
});
- // Attach our own handler for onbeforeunload which respects the current one
- var otherOnBeforeUnload = window.onbeforeunload;
- function ourOnBeforeUnload() {
- var fallbackResult, retval;
+ var savedWindowOnBeforeUnload;
+ $( window )
+ .on( 'beforeunload.editwarning', function () {
+ var retval;
- // Check if someone already set an onbeforeunload hook
- if ( otherOnBeforeUnload ) {
- // Get the result of their onbeforeunload hook
- fallbackResult = otherOnBeforeUnload();
- }
-
- // Check if their onbeforeunload hook returned something
- if ( fallbackResult !== undefined ) {
- // Exit here, returning their message
- retval = fallbackResult;
- } else {
// Check if the current values of some form elements are the same as
// the original values
if (
@@ -37,33 +26,29 @@
// Return our message
retval = mw.msg( 'vector-editwarning-warning' );
}
- }
-
- // Unset the onbeforeunload handler so we don't break page caching in Firefox
- window.onbeforeunload = null;
- if ( retval !== undefined ) {
- // ...but if the user chooses not to leave the page, we need to rebind it
- setTimeout( function () {
- window.onbeforeunload = ourOnBeforeUnload;
- }, 1 );
- return retval;
- }
- }
-
- // Do the first binding now
- window.onbeforeunload = ourOnBeforeUnload;
- function pageShowHandler() {
- // Re-add onbeforeunload handler on pageshow.
- window.onbeforeunload = ourOnBeforeUnload;
- }
-
- $( window ).on( 'pageshow', pageShowHandler );
+ // Unset the onbeforeunload handler so we don't break page caching in Firefox
+ savedWindowOnBeforeUnload = window.onbeforeunload;
+ window.onbeforeunload = null;
+ if ( retval !== undefined ) {
+ // ...but if the user chooses not to leave the page, we need to rebind it
+ setTimeout( function () {
+ window.onbeforeunload = savedWindowOnBeforeUnload;
+ }, 1 );
+ return retval;
+ }
+ } )
+ .on( 'pageshow.editwarning', function () {
+ // Re-add onbeforeunload handler
+ if ( window.onbeforeunload == null ) {
+ window.onbeforeunload = savedWindowOnBeforeUnload;
+ }
+ } );
// Add form submission handler
$( '#editform' ).submit( function () {
- // Restore whatever previous onbeforeload hook existed
- window.onbeforeunload = otherOnBeforeUnload;
+ // Unbind our handlers
+ $( window ).off( '.editwarning' );
});
});