1

I want to capture the browser window/tab close event. I have tried the following But not working:

<script type="text/javascript">
window.onbeforeunload = function(){ myUnloadEvent(); }
function myUnloadEvent() {
    alert ('Calling some alert messages here');
}
</script>
<body>
Body of the page goes here.
</body>

Tried these links also but no more success.

javascript to check when the browser window is close
How to capture the browser window close event?
javascript detect browser close tab/close browser
Trying to detect browser close event

Problem :

I just want to detect the browser close event and do some handling on that without showing the prompt to the user.

I have tried many methods to detect browser close event through jQuery or JavaScript. But unfortunately I could not succeed. The onbeforeunload and onunload methods are also not working.

Any help will be highly appreciated. Thanks

Community
  • 1
  • 1
Debug Diva
  • 26,058
  • 13
  • 70
  • 123

2 Answers2

1

You can see the console quickly write that line before the screen closes.

window.onbeforeunload = myUnloadEvent;
function myUnloadEvent() {
    console.log("Do your actions in here")
}

Alerts are not allowed during beforeunload - if you have Preserve log switched on you will see this:

Blocked alert('Calling some alert messages here') during beforeunload.

But if you use a console.log command, it will go through.

philz
  • 1,012
  • 6
  • 11
  • @emmaaaaah. So there is any way to only detect the browser close event. So that, i can perform some operation..i don't want to show any dialogue to the user. – Debug Diva May 26 '15 at 17:14
1

You cannot show an alert() on an onbeforeunload. You need to return to show a confirm dialog.

<script type="text/javascript">
window.onbeforeunload = function(e){  
  return 'Calling some alert messages here'; //return not alert
}
</script>
<body>
Body of the page goes here.
</body>

Also do not call a function like that, write directly in the onbeforeunloadOR return myUnloadEvent(); It depends what you are trying to do inside onbeforeunload. Try not to make any AJAX call as the browser may not run the script. You can unset web storage varibles, delete session, etc.

Also beware that this event will also fire when you refresh the page.

Zee
  • 8,420
  • 5
  • 36
  • 58
  • The user said 'I just want to detect the browser close event and do some handling on that without showing the prompt to the user', meaning no return. Also, try putting this into the console - nothing happens because youre calling myUnloadEvent but not returning what it is returning, essentially doing nothing. `return myUnloadevent()` would work. – philz May 26 '15 at 04:55
  • @emmaaaah Yeah, but OP also claims that the methods aren't being called because they don't see an alert, which is because they're doing it wrongly. – Ja͢ck May 26 '15 at 05:02
  • @emmaaaah. Misread that part. Code updated. Also it depends what you are trying to do inside the `onbeforeunload`, as the browser may or may not rut the script – Zee May 26 '15 at 05:06
  • @Zee. So there is any way to only detect the browser close event. So that, i can perform some operation..i don't want to show any dialogue to the user. – Debug Diva May 26 '15 at 17:14
  • @zee as you said, "Try not to make any AJAX call as the browser may not run the script". Yes, I agree on those some time work sometimes not. My requirement is to call AJAX request every time when the page reloads or browser closes, and also call event without alert box etc. What should I use for that? – mahesh kajale Feb 09 '18 at 14:59