<script>
function report( event ) {
var payload = {
+ source: "jQuery onbeforeunload iframe test",
event: event.type
};
- return parent.postMessage( JSON.stringify(payload), "*" );
+ return parent.postMessage( JSON.stringify( payload ), "*" );
}
jQuery( window ).on( "beforeunload", function( event ) {
report( event );
- }).on( "load", function( event ) {
- setTimeout(function() {
+ } ).on( "load", function( event ) {
+ setTimeout( function() {
window.location.reload();
- }, 50);
- });
+ }, 50 );
+ } );
</script>
</html>
var done = assert.async();
window.onmessage = function( event ) {
- var payload = JSON.parse( event.data );
+ try {
+ var payload = JSON.parse( event.data );
- assert.ok( payload.event, "beforeunload", "beforeunload event" );
+ // Ignore unrelated messages
+ if ( payload.source === "jQuery onbeforeunload iframe test" ) {
+ assert.ok( payload.event, "beforeunload", "beforeunload event" );
- iframe.remove();
- window.onmessage = null;
- done();
+ iframe.remove();
+ window.onmessage = null;
+ done();
+ }
+ } catch ( e ) {
+
+ // Messages may come from other sources, like browser extensions;
+ // some may not be valid JSONs and thus cannot be `JSON.parse`d.
+ }
};
iframe.appendTo( "#qunit-fixture" );