当标签被关闭清除会话变量变量、标签

2023-09-11 00:41:20 作者:丿遗失De美好灬

我需要清除会话变量时,标签被关闭,但我找不到任何解决办法为止。我有最接近的是通过使用onbeforeunload的功能的JavaScript。

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.

<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.

推荐答案

没有处理你在找什么没有安全的方式。 onbeforunload事件执行每次你离开页面的时间。 (我有这样的昨天类似的问题对我的项目之一)。你可以得到的最接近的是,以控制用户离开页面。

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.

请参阅此link在一些评论张贴局域网。并检查了答案的 丹尼尔·梅洛 这是接近你可以使用该解决方案得到从这个问题。

See this link posted by lan in some of the comments. And check the answer by Daniel Melo That is as close you can get with the solution from this problem.

我也发现了这个 链接 但其基本的计算器给出的答案的提取。

I also found this link but its basically the extraction of the answers given in stackoverflow.

希望这有助于。