]> source.dussan.org Git - jquery.git/commitdiff
Event: Allow triggerHandler(beforeunload)
authorDave Methvin <dave.methvin@gmail.com>
Tue, 4 Mar 2014 03:43:51 +0000 (22:43 -0500)
committerDave Methvin <dave.methvin@gmail.com>
Wed, 5 Mar 2014 02:50:41 +0000 (21:50 -0500)
Fixes #14791
(cherry picked from commit 06adf7c95d93507908181995a9ea15fe2729595a)

Conflicts:
src/event.js

src/event.js
test/data/event/triggerunload.html [new file with mode: 0644]
test/unit/event.js

index 307907aa0d923306baac1870bc38302d42fb1680..e2bac6f9be65d907a6f9d7e081cfd5e05a902a09 100644 (file)
@@ -616,8 +616,9 @@ jQuery.event = {
                beforeunload: {
                        postDispatch: function( event ) {
 
-                               // Even when returnValue equals to undefined Firefox will still show alert
-                               if ( event.result !== undefined ) {
+                               // Support: Firefox 20+
+                               // Firefox doesn't alert if the returnValue field is not set.
+                               if ( event.result !== undefined && event.originalEvent ) {
                                        event.originalEvent.returnValue = event.result;
                                }
                        }
diff --git a/test/data/event/triggerunload.html b/test/data/event/triggerunload.html
new file mode 100644 (file)
index 0000000..a7879c7
--- /dev/null
@@ -0,0 +1,18 @@
+<!doctype html>
+<html>
+       <script src="../../jquery.js"></script>
+       <script>
+               var called = false,
+                       error = false;
+
+               window.onerror = function() { error = true; };
+
+               jQuery( window ).on( "beforeunload", function( event ) {
+                       called = true;
+                       return "maybe";
+               }).on( "load", function( event ) {
+                       $( window ).triggerHandler( "beforeunload" );
+                       window.parent.iframeCallback( called && !error );
+               });
+       </script>
+</html>
index 2a9cd22f3a9a85fd8232d6e23dcc2288915d641a..0cd181252e40dafef10c86282c6c60b41121e157 100644 (file)
@@ -2522,6 +2522,11 @@ testIframeWithCallback( "Focusing iframe element", "event/focusElem.html", funct
        ok( isOk, "Focused an element in an iframe" );
 });
 
+testIframeWithCallback( "triggerHandler(onbeforeunload)", "event/triggerunload.html", function( isOk ) {
+       expect( 1 );
+       ok( isOk, "Triggered onbeforeunload without an error" );
+});
+
 // need PHP here to make the incepted IFRAME hang
 if ( hasPHP ) {
        testIframeWithCallback( "jQuery.ready synchronous load with long loading subresources", "event/syncReady.html", function( isOk ) {