Index: trunk/extensions/UsabilityInitiative/EditWarning/EditWarning.js |
— | — | @@ -1,19 +1,43 @@ |
2 | 2 | /* JavaScript for EditWarning extension */ |
3 | 3 | |
4 | 4 | $( document ).ready( function() { |
| 5 | + // Checks if the skin is not vector |
5 | 6 | if( skin != 'vector' ) |
| 7 | + // Exits |
6 | 8 | return; |
7 | | - |
| 9 | + // Gets the original values of some form elements |
8 | 10 | $( '#wpTextbox1, #wpSummary' ).each( function() { |
9 | 11 | $(this).data( 'origtext', $(this).val() ); |
10 | 12 | }); |
11 | | - if( !( 'onbeforeunload' in window ) ) |
12 | | - window.onbeforeunload = function() { |
13 | | - if( $( '#wpTextbox1' ).data( 'origtext' ) != $( '#wpTextbox1' ).val() || |
14 | | - $( '#wpSummary' ).data( 'origtext' ) != $( '#wpSummary' ).val() ) |
15 | | - return gM( 'editwarning-warning' ); |
16 | | - }; |
| 13 | + |
| 14 | + fallbackWindowOnBeforeUnload = window.onbeforeunload; |
| 15 | + window.onbeforeunload = function() { |
| 16 | + var fallbackResult = null; |
| 17 | + // Checks if someone already set on onbeforunload hook |
| 18 | + if ( fallbackWindowOnBeforeUnload ) { |
| 19 | + // Gets the result of their onbeforeunload hook |
| 20 | + fallbackResult = fallbackWindowOnBeforeUnload(); |
| 21 | + } |
| 22 | + // Checks if their onbeforeunload hook returned something |
| 23 | + if ( fallbackResult !== null ) { |
| 24 | + // Exits here, returning their message |
| 25 | + return fallbackResult; |
| 26 | + } |
| 27 | + // Checks if the current values of some form elements are the same as |
| 28 | + // the original values |
| 29 | + if( |
| 30 | + $( '#wpTextbox1' ).data( 'origtext' ) != $( '#wpTextbox1' ).val() || |
| 31 | + $( '#wpSummary' ).data( 'origtext' ) != $( '#wpSummary' ).val() |
| 32 | + ) { |
| 33 | + // Returns our message |
| 34 | + return gM( 'editwarning-warning' ); |
| 35 | + } |
| 36 | + } |
| 37 | + // Adds form submission handler |
17 | 38 | $( 'form' ).submit( function() { |
18 | | - window.onbeforeunload = function() {}; |
| 39 | + // Restores whatever previous onbeforeload hook existed |
| 40 | + window.onbeforeunload = fallbackWindowOnBeforeUnload; |
19 | 41 | }); |
20 | 42 | }); |
| 43 | +//Global storage of fallback for onbeforeunload hook |
| 44 | +var fallbackWindowOnBeforeUnload = null; |
\ No newline at end of file |