当浏览器标签关闭时,我需要清除会话变量,但是目前我还没有找到任何解决方案。 我最接近的方法是使用JavaScript中的“onbeforeunload”函数。
<body onbeforeunload='destroySession()'>
<!--Codes for the site includeing php scripts-->
</body>
<script type='text/javascript'>
function destroySession(){
$.ajax({
url: "destroySession.php"
});
}
<script>
问题在于每次点击新链接、刷新页面,甚至提交表单时都会调用该函数。有没有更好的方法在关闭标签页时销毁会话变量?还是我做错了什么?请帮忙。