javascript 关闭选项卡时清除 SESSION 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18276226/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Clearing SESSION variables when a tab is closed
提问by Bhuns
I need to clear the session variables when the tab is closed but I could not find any solutions so far. The closest I have got is by using the "onbeforeunload" function is javascript.
我需要在选项卡关闭时清除会话变量,但到目前为止我找不到任何解决方案。我得到的最接近的是使用“onbeforeunload”函数是 javascript。
<body onbeforeunload='destroySession()'>
<!--Codes for the site includeing php scripts-->
</body>
<script type='text/javascript'>
function destroySession(){
$.ajax({
url: "destroySession.php"
});
}
<script>
The problem is the function is called every time a new link is clicked, refreshed or even if a form is submitted. Is there a better way to destroy session variables on closing a tab or am I doing something wrong? Please help.
问题是每次单击、刷新或提交表单时都会调用该函数。有没有更好的方法在关闭选项卡时销毁会话变量,或者我做错了什么?请帮忙。
回答by dishwasherWithProgrammingSkill
There is no secure way of handling what you are looking for. onbeforunload event executes every time you leave the page. (I had similar problem like this yesterday for one of my projects). The closest you can get is to control how the users leave the page.
没有安全的方法来处理您正在寻找的内容。每次离开页面时都会执行 onbeforunload 事件。(昨天我的一个项目遇到了类似的问题)。最接近的是控制用户离开页面的方式。
See this linkposted by lan in some of the comments. And check the answer by Daniel MeloThat is as close you can get with the solution from this problem.
请参阅lan 在某些评论中发布的此链接。并检查Daniel Melo的答案这与您从这个问题中获得的解决方案最接近。
I also found this linkbut its basically the extraction of the answers given in stackoverflow.
我也找到了这个链接,但它基本上是对 stackoverflow 中给出的答案的提取。
Hope this helps.
希望这可以帮助。
回答by Max Malyk
Unfortunately, there is no way to prevent page refresh (caused by form submit or any other navigation) from calling "onbeforeunload".
不幸的是,没有办法阻止页面刷新(由表单提交或任何其他导航引起)调用“onbeforeunload”。
回答by Yatin Trivedi
Yep you can do it,
是的,你可以做到,
</head>
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
var validNavigation = false;
function endSession() {
// Browser or broswer tab is closed
// Do sth here ...
alert("bye");
}
function wireUpEvents() {
/*
* For a list of events that triggers onbeforeunload on IE
* check http://msdn.microsoft.com/en-us/library/ms536907(VS.85).aspx
*/
window.onbeforeunload = function() {
if (!validNavigation) {
endSession();
}
}
// Attach the event keypress to exclude the F5 refresh
$(document).bind('keypress', function(e) {
if (e.keyCode == 116){
validNavigation = true;
}
});
// Attach the event click for all links in the page
$("a").bind("click", function() {
validNavigation = true;
});
// Attach the event submit for all forms in the page
$("form").bind("submit", function() {
validNavigation = true;
});
// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function() {
validNavigation = true;
});
}
// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {
wireUpEvents();
});
</script>
</head>
<body>
<h1>Eureka!</h1>
<a href="http://www.google.com">Google</a>
<a href="http://www.yahoo.com">Yahoo</a>
</body>
</html>