本文介绍了关闭浏览器时删除Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在关闭浏览器并已将其放在window.onbeforeunload上时删除cookie,如下所示:

i need to delete cookie when browser is closed and already put on window.onbeforeunload along with other as following:

 window.onbeforeunload = function(){
        location.replace("admin.jsp?action=logout");
        deleteCookie('Times');
 }

使用setCookie如下:

with setCookie as following:

 function setCookie(name,value,days) {
         var expires;
         if (days) {
                  var date = new Date();
                  date.setTime(date.getTime()+(days*24*60*60*1000));
                  expires = "; expires="+date.toGMTString();
         }
         else {
                  expires = "";
              }
                  document.cookie = name+"="+value+expires+"; path=/";
     }

当涉及deleteCookie时,它包含以下内容:

when it comes to deleteCookie, it contains as below:

    setCookie(name,value,-1);

问题是,每当我重新启动浏览器时,它总是出现在window.onbeforeunload中,从而触发了deleteCookie.实际上,我每当用户登录时都需要使用此方法来重置倒数计时器,因为如果计数器没有结束并且用户有时会在结束之前关闭窗口/选项卡,则cookie永远不会删除.因此,我的想法是要么在用户登录时重置计数器,要么在用户退出时仅删除cookie.但是,我仍然不知道该如何编码.谁能帮我吗?代码片段将不胜感激.CMIIW

The problem is whenever i restart browser, it always comes to window.onbeforeunload so that deleteCookie fired. I actually need this to reset my countdown timer whenever user logs in since the cookie never deleted if the counter doesn't end and user sometimes closes window/tab before it ends. So, my idea is either reset the counter when user logs in or simply delete cookie when user logs out. I still can't figure out how to code this, however. Can anyone help me out? Code snippet will be appreciated, though. CMIIW

推荐答案

在cookie上未指定Expires值将导致在浏览器会话结束(即用户关闭浏览器)时删除cookie.喜欢:

Not specifying Expires value on the cookie will cause the cookie to be removed when the browser session ends i.e. when user closes the browser. Like:

Set-Cookie: made_write_conn=1295214458; Path=/; Domain=.foo.com

made_write_conn cookie made_write_conn没有有效期,因此成为会话cookie.用户关闭浏览器后,它将被删除.尝试做:

The made_write_conn cookie made_write_conn does not have an expiration date, making it a session cookie. It will be deleted after the user closes his/her browser.Try doing:

setCookie('Times',value, '');

这篇关于关闭浏览器时删除Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:43