4

I want to show alert on only on close tab/window.

Right now i have did something as shown bellow, but it fires even on 'F5', back button of browser... but I only want it on tab/window close.

I tried so much on this and also checked so many stackoverflow links but didn't get any solution

window.onbeforeunload = function(e) {
                return confirmExit()
            }
            function confirmExit() {
                //my logic
                return false;
            }

also added following code to prevent onbeforeunload on click of any link in my site

$(document).ready(function() {
                $('a[rel!=ext]').click(function() {
                    window.onbeforeunload = null;
                });
                $('form').submit(function() {
                    window.onbeforeunload = null;
                });
            });

so please help me out.

Er.KT
  • 2,852
  • 1
  • 36
  • 70
  • 1
    possible duplicate of [Detect Close windows event by Jquery](http://stackoverflow.com/questions/16707249/detect-close-windows-event-by-jquery) – JF it Sep 23 '14 at 11:56
  • @JFit i know it duplicate question but there may be any way like prevent 'F5' with keycode or anything like that? – Er.KT Sep 23 '14 at 11:58
  • Oh, why didnt you say so - Possible duplicate of: http://stackoverflow.com/questions/2482059/disable-f5-and-browser-refresh-using-javascript ;) @Er.KT – JF it Sep 23 '14 at 12:00
  • @JFit great buddy :), but now want same for browser back button ? – Er.KT Sep 23 '14 at 12:06
  • Not much out there on back buttons.. perhaps one of these? : http://stackoverflow.com/questions/3243684/disable-back-button-in-browser-using-jquery – JF it Sep 23 '14 at 12:14
  • @Er.KT You can listen for the 'keydown' event for f5 (a bit trickier for Ctrl+R), but this won't help for the refresh button – yoelp Sep 23 '14 at 21:57
  • @yoelp yes facing same issue :( – Er.KT Sep 24 '14 at 04:55
  • See this new question: (http://stackoverflow.com/questions/26011548/how-to-track-browser-refresh-back-tab-close-and-browser-close-event-in-javascrip) – Martin Ernst Sep 24 '14 at 08:40

1 Answers1

2

For security reasons this is not, or should not be, possible. In order to deterministically know when and how the user interacts in with the User Agent (browser), you (should) have to have access to the higher security context of the browser. Page-scripts are supposed to be specifically blocked from having this access.

You get to know when the page is going to be unloaded (onbeforeunload, and onunload events) but not the explicit reason for the page being unloaded.

Note that you can and should handle this event through window.addEventListener() and the beforeunload event. Using window.onbeforeunload and window.onunload instead of window.addEventListener() is much more limiting and does not allow sharing the event with other scripts which might be loaded, nor removing just your own event handler. You should be in the habit of using window.addEventListener() and a named function in order to play well in an environment where you are not limited to having just your own script running on a page.

Makyen
  • 31,849
  • 12
  • 86
  • 121