2

Possible Duplicate:
window.onbeforeunload not working in chrome

Am facing a problem with handling the browser's window / tab close event in javascript. I tried and googled many cases to trigger my function before closing the browser's window / tab.

In my function i should display a simple alert message as below:

 function doBeforeClose(){
 // do work needed
 alert("you have successfully logged out");
 }

I tried to trigger this function by

<body onbeforeunload="doBeforeClose()">

or

window.onbeforeunload = function(evt){ .... }

I am able do call this function in IE and Mozilla, but not in Chrome. I got to know that there is a bug in chrome for capturing this event (may be a bug or a feature for security issues).

Even this doesn't fulfill my requirement as this event should trigger only on close event, not page unload.

i.e, its getting called on refresh / redirection of the pages.

To avoid this on refresh i captured events for F5 and Ctrl_R, but not able to capture refresh button event.

I tried to capture refresh button event by getting mouse coordinates, but it is not at all recommended option as all browser's alignment of buttons wont be same.

Please do needful.

Community
  • 1
  • 1
NamingException
  • 2,388
  • 1
  • 19
  • 41
  • 1
    presently even if you provide me the solution for IE / Mozilla, it will be great help to me... event should be triggered only on close event, not on refresh – NamingException Oct 11 '12 at 08:03
  • Page reload is a browser event, not a documented event. Therefore, I don't think there is a JavaScript listener for that. Try setting a cookie or using `localStorage` to "remember" that user was already on given page. That will allow you to know when he reenters it. – Konrad Dzwinel Oct 11 '12 at 08:11
  • 1
    If my understanding is correct, the cookie/localstorage will check after the function is called and page is reloaded. By the time i'll invalidate the session. so, will it check before fuction is called or before reloading page... Please, give some brief idea... :) – NamingException Oct 11 '12 at 08:19
  • Sorry, I misunderstood you. You want to know if user navigated to another page or just reloaded current one when `onbeforeunload` event fires. I googled around and this question appeared couple of times on stackoverflow (http://stackoverflow.com/questions/1271162/get-refresh-event-from-page, http://stackoverflow.com/questions/6627427/how-detect-refresh-event-with-prototype), but no reasonable answer was given. – Konrad Dzwinel Oct 11 '12 at 08:33
  • Oh, okay... thank you for your comments / suggestions Konrad... – NamingException Oct 11 '12 at 09:15

0 Answers0