最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Find browser closerefresh using javascriptjquery - Stack Overflow

programmeradmin1浏览0评论
  1. Could someone explain if there is a way to differentiate between browser close and refresh events using javascript/jquery.
    Basically, I need to log-out the user if he is closes the tab.

    I tried using jQuery .unload(). But that is firing for Refresh and Form Submit. I tried assigning a global variable and avoided .unload() during Form Submit. But how this can be avoided for Refresh, or if there is any other work-around to achieve this..

  2. I wonder if the same can be done only if the window is closed(instead of tab).

  1. Could someone explain if there is a way to differentiate between browser close and refresh events using javascript/jquery.
    Basically, I need to log-out the user if he is closes the tab.

    I tried using jQuery .unload(). But that is firing for Refresh and Form Submit. I tried assigning a global variable and avoided .unload() during Form Submit. But how this can be avoided for Refresh, or if there is any other work-around to achieve this..

  2. I wonder if the same can be done only if the window is closed(instead of tab).

Share Improve this question asked May 13, 2011 at 16:23 RaghavRaghav 3,0687 gold badges41 silver badges61 bronze badges 9
  • i think there is only the unload event – ave4496 Commented May 13, 2011 at 16:26
  • you might have to try and find out who is aksing the window to close. if source is button submit then dont log out..?! or some logic to log straight back in if the page relaods?? But the best thing is to keep some sort of httpSession – Piotr Kula Commented May 13, 2011 at 16:29
  • This might be better implemented with cookies: refresh will keep the cookie, close will end the session. Then use the cookie var to determine what you want to do. – kei Commented May 13, 2011 at 16:29
  • Yea or Cookies! i always forget about them – Piotr Kula Commented May 13, 2011 at 16:30
  • you can detect the users F5 button, that about it, but use serverside to pass to js referer url, and see if it was your domain or not – Val Commented May 13, 2011 at 16:30
 |  Show 4 more ments

4 Answers 4

Reset to default 2

You could try using an instance cookie, ie one that is delete once the browser is closed, you could then check for the cookies existence from JavaScript. otherwise I don't think there is a way to tell the difference your looking for with just javascript.

Unfortunately, JavaScript only provides onunload, which will fire whenever the user navigates away from, closes or reloads the current page.

Robert is correct, this is a good situation for session cookie: if you create a cookie without specifying an Expiration, it will expire when the tab/window is closed by the user.

You might not be able to do that other than some cookie based methods like non persistent cookies which is a work around. Second, it is not that easy to differentiate a page refresh, form based page redirect unload, or browser close. Its difficult to identify as far as I know.

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.

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/en-US/docs/Web/Reference/Events/beforeunload?redirectlocale=en-US&redirectslug=DOM/Mozilla_event_reference/beforeunload

Sadly, the implementation of onunload or onbeforeunload isn't that great between browsers and in cases of a crash the unload event will never be fired. You're best bet is to not worry about catching the unload events and just have sensible session expiration times.

If you didn't have to worry about distinguish between form submits and refreshes you could could get pretty good coverage with onunload, but it still wouldn't be 100%.

发布评论

评论列表(0)

  1. 暂无评论