Fix #12061. Avoid window.onbeforeunload to permit multiple handlers. Close gh-894.

This commit is contained in:
Oleg 2012-08-15 00:10:10 +04:00 committed by Dave Methvin
parent 08341437e0
commit 9dd0b01017
2 changed files with 56 additions and 18 deletions

View File

@ -549,16 +549,11 @@ jQuery.event = {
}, },
beforeunload: { beforeunload: {
setup: function( data, namespaces, eventHandle ) { postDispatch: function( event ) {
// We only want to do this special case on windows
if ( jQuery.isWindow( this ) ) {
this.onbeforeunload = eventHandle;
}
},
teardown: function( namespaces, eventHandle ) { // Even when returnValue equals to undefined Firefox will still show alert
if ( this.onbeforeunload === eventHandle ) { if ( event.result !== undefined ) {
this.onbeforeunload = null; event.originalEvent.returnValue = event.result;
} }
} }
} }

View File

@ -1369,20 +1369,63 @@ test("Submit event can be stopped (#11049)", function() {
form.remove(); form.remove();
}); });
test("on(beforeunload) creates/deletes window property instead of adding/removing event listener", function() { // Test beforeunload event only if it supported (i.e. not Opera)
expect(3); if ( window.onbeforeunload === null ) {
asyncTest("on(beforeunload)", 4, function() {
var doc,
forIE6 = 0,
iframe = jQuery("<iframe src='data/iframe.html' />");
equal( window.onbeforeunload, null, "window property is null/undefined up until now" ); iframe.appendTo("#qunit-fixture").one( "load", function() {
doc = iframe[ 0 ].contentWindow || iframe[ 0 ].contentDocument;
var handle = function () {}; jQuery( doc ).on( "beforeunload", function() {
jQuery(window).on( "beforeunload", handle ); ok( true, "beforeunload event is fired" );
});
equal( typeof window.onbeforeunload, "function", "window property is set to a function"); strictEqual( doc.onbeforeunload, null, "onbeforeunload property on window object still equals null" );
jQuery(window).off( "beforeunload", handle ); jQuery( doc ).on( "beforeunload", function() {
equal( window.onbeforeunload, null, "window property has been unset to null/undefined" ); // On iframe in IE6 beforeunload event will not fire if event is binded through window object,
}); // nevertheless, test should continue
window.setTimeout(function() {
if ( !forIE6 ) {
checker();
}
});
});
doc.onbeforeunload = function() {
if ( !forIE6 ) {
forIE6++;
checker();
}
};
function checker() {
ok( true, "window.onbeforeunload handler is called" );
iframe = jQuery("<iframe src='data/iframe.html' />");
iframe.appendTo("#qunit-fixture").one( "load", function() {
doc = iframe[ 0 ].contentWindow || iframe[ 0 ].contentDocument;
jQuery( doc ).on( "beforeunload", function() {
strictEqual( doc.onbeforeunload, null, "Event handler is fired, even when onbeforeunload property on window is nulled" );
start();
});
doc.onbeforeunload = null;
doc.location.reload();
});
}
doc.location.reload();
});
});
}
test("jQuery.Event( type, props )", function() { test("jQuery.Event( type, props )", function() {