1

如果我的页面正在导航到其他页面或关闭,我想删除所有 cookie,否则我想让页面正常刷新。这样做的原因是Clearing cookie for tabs

所以我的问题是:
我们如何才能知道页面是刷新还是导航或关闭?

我尝试使用

window.onbeforeunload = function() {
    var old_url= window.location.href;
    var new_url=  /* I dont know how to get new URL here */;
    if(old_url == new_url){
      return true;
    }
    else {
      return false;
    }
}

它不起作用:(
还有其他方法吗?

4

3 回答 3

1

我在javascript中声明了一个全局变量。

<script type="text/javascript">
  var is_refresh_true = true;
</script>

每当我尝试使用 javascript 刷新页面时,我只是将变量值更改为 false
例如:

<script type="text/javascript">
   function Callme(){  
      is_refresh_true = false;
      window.location.href = window.location.href;
    }
</script>

还有身体的 onUnload 事件:

 function DeleteCokies() {            
     if (is_refresh_true) {
            deleteAllCookies();
     }
 }

Its not a perfect solution but works for me.
Still I am waiting for perfect answer. Please reply if anyone finds it.

于 2012-09-17T14:25:47.550 回答
0

If you don't set any Expiration for the cookie, it will be cleared with the session(ie., when you close the browser)..

If you are navigating to other pages, I don't see any problem with those cookies on other pages.. and suppose if you are coming back to the same page again, it will show the same tab..

于 2012-09-17T14:44:05.037 回答
0
var confirmOnPage = function () {

   function del_cookie(name)
   {
       document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
   }

};

window.onbeforeunload = confirmOnPage;

于 2013-10-10T12:57:17.200 回答