admin管理员组文章数量:1391999
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..I wonder if the same can be done only if the window is closed(instead of tab).
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..I wonder if the same can be done only if the window is closed(instead of tab).
- 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
4 Answers
Reset to default 2You 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%.
本文标签: Find browser closerefresh using javascriptjqueryStack Overflow
版权声明:本文标题:Find browser closerefresh using javascriptjquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744711458a2621154.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论