admin管理员组

文章数量:1389768

I need to clear some session variables whenever user closes the browser window. The window can be closed in two ways:

  • the normal way by pressing the window close button
  • by pressing a custom button inside the window

I am using jQuery, so the listener is:

// clear some session variables
$(window).bind('beforeunload', function() {
    makeAjaxCall(some, params);
});

The window is closed by calling a function that eventually executes:

window.close();

The problem is that the second way to close the window only triggers the beforeunload event in FF. In Chrome the second way works only sometimes, in Safari it does not work at all. Any thoughts?


SOLUTION (so far):

// clear search filter whenever the user closes the window
$(window).on('beforeunload', function() {   // most browsers except Safari
    doSomeMagic(param);
    return;
}).on('unload', function() {         // Chrome
    doSomeMagic(param);
});

This works in most browsers: IE8+, Chrome, FF when closing window with window.close() and/or native browser button

It also works in Safari if window is closed by native browser window.

Does not work in Opera at all.

I need to clear some session variables whenever user closes the browser window. The window can be closed in two ways:

  • the normal way by pressing the window close button
  • by pressing a custom button inside the window

I am using jQuery, so the listener is:

// clear some session variables
$(window).bind('beforeunload', function() {
    makeAjaxCall(some, params);
});

The window is closed by calling a function that eventually executes:

window.close();

The problem is that the second way to close the window only triggers the beforeunload event in FF. In Chrome the second way works only sometimes, in Safari it does not work at all. Any thoughts?


SOLUTION (so far):

// clear search filter whenever the user closes the window
$(window).on('beforeunload', function() {   // most browsers except Safari
    doSomeMagic(param);
    return;
}).on('unload', function() {         // Chrome
    doSomeMagic(param);
});

This works in most browsers: IE8+, Chrome, FF when closing window with window.close() and/or native browser button

It also works in Safari if window is closed by native browser window.

Does not work in Opera at all.

Share Improve this question edited Feb 6, 2014 at 10:47 DeadMoroz asked Feb 5, 2014 at 11:43 DeadMorozDeadMoroz 2701 gold badge4 silver badges13 bronze badges 7
  • onbeforeunload behaviour is not consistent across browser. Simple answer is you cannot set any logic based on this event except just make it returning a string to show a prompt dialog to user (even in FF, the custom message wouldn't be displayed). There was some workaround as setting ajax request to sync but chrome doesn't seem to like it and on recent chrome update, no more seems to work anyway – A. Wolff Commented Feb 5, 2014 at 11:45
  • But how e it works by closing the window by pressing the X on window across all browsers? The problems only start when I need to use JS to close the window. Shouldn't window.close() trigger the beforeunload event? I event tried to manually trigger it and it worked in Chrome, but still no luck in Safari. – DeadMoroz Commented Feb 5, 2014 at 12:05
  • So why not instead of calling window.close();, call directly makeAjaxCall() and close window inside ajax request success callback or using promise method. But i'm still not convinced that closing the window using the native close button and then firing onbeforeunload event will work cross browser. I mean for sure the event is fired but onunload event of browser doesn't wait for onbeforeunload event to be pleted before beeing called. So, you have no way to be sure the ajax request has pleted – A. Wolff Commented Feb 5, 2014 at 12:13
  • try this $(window).on('beforeunload', function () { alert("Bye now!"); }); – Rahul Kumar Commented Feb 5, 2014 at 12:33
  • @DeadMoroz stackoverflow./a/21577501/1131841 – Rahul Kumar Commented Feb 5, 2014 at 12:38
 |  Show 2 more ments

1 Answer 1

Reset to default 3

Try this

$(window).on('beforeunload', function(){
  return 'Are you sure you want to leave?';
});

$(window).on('unload', function(){
     logout();

});

This solution works in all browsers and I have tested it.

本文标签: javascriptwindowclose() and the beforeunload eventStack Overflow