0

Please help me to find event for mozilla firefox. I want to show alert message and remove session from server side when I close browser by click cross button , when close tab and when click back button. I find event such as "onbeforeunload". But there is problem like this event also run when I refresh page also.

  • 1
    I don't think browsers make a distinction. They fire `onbeforeunload` (and after that `onunload`) when the page is being navigated away from, but they don't care what triggered that, be it closing the browser, closing the tab, refreshing, following a link, etc – Anthony Grist Dec 12 '12 at 15:20

2 Answers2

0
window.onbeforeunload

How to detect the window(new tab) close event?

Hope this helps.

Community
  • 1
  • 1
Ciack404
  • 196
  • 1
  • 2
  • 13
  • 1
    Note however that sending an ajax request on window.onbeforeunload will sometimes not reach the server. – Kevin B Dec 12 '12 at 15:19
  • @KevinB I didn't knew that. Thanks for the info :) Is there a rule or it depends on browser/timing/network speed-latency... – Ciack404 Dec 12 '12 at 15:24
  • But Ciack404, I already mention that onbeforeunload event also run when page refresh which I don't want. – user1898213 Dec 12 '12 at 15:25
0

You might not be able to do that other than some cookie based methods which is development work arounds like non persistent cookies. You might not know correctly any time whether it is a back button, next page redirect, form based redirect action, or browser close. Even the implementation might not direct.

you can do some small things before the customer closes the tab. javascript detect browser close tab/close browser but if your list of actions are big and the tab closes before it is finished you are helpless. You can try it but with my experience donot depend on it. Yes, you cannot at this time differentiate back and refresh and close.

window.addEventListener("beforeunload", function (e) {
  var confirmationMessage = "\o/";
  /* Do you small action code here */
  (e || window.event).returnValue = confirmationMessage; //Gecko + IE
  return confirmationMessage;                            //Webkit, Safari, Chrome
});

https://developer.mozilla.org/en-US/docs/Web/Reference/Events/beforeunload?redirectlocale=en-US&redirectslug=DOM/Mozilla_event_reference/beforeunload

Community
  • 1
  • 1
Gary
  • 2,293
  • 2
  • 25
  • 47