js删除cookie不成功

最近在做一个项目,js删除cookie老是不成功,最后发现是没有指定cookie的域

js删除cookie其实是改变cookie的过期时间,将cookie的过期时间设置为过去,以下几项是必须要指定的

1.cookie名

2.cookie值

3.cookie过期时间

4.cookie域

5.cookie路径

以上几项缺一不可,必须都要有才能修改cookie

具体代码如下:

 1 /**设置cookie
 2      * @param {cookie名}
 3      * @param {cookie值}
 4      * @param {设置cookie域}
 5      * @param {过期时间(天)} [varname]
 6      */
 7     function setCookie(name, value, domain, day) {
 8         var date = new Date();
 9 
10         date.setTime(date.getTime()+1000*60*60*24*day);
11         document.cookie = name+'='+value+';expires='+date+';domain='+domain+';path=/';
12     }
13 
14     /**
15      * 获取cookie
16      * @return {[type]} [description]
17      */
18     function getCookie(name) {
19         var data = document.cookie.split(';'),
20             param = {};
21         for (var i = 0; i < data.length; i++) {
22             param[data[i].split('=')[0].replace(/\s/,'')] = data[i].split('=')[1];
23         }
24         return param[name];
25     }
26 
27     /**
28      * 删除cookie
29      * name: 删除cookie名
30      * domain: 所在的域
31      */
32     function delCookie(name, domain) {
33         document.cookie = name+'='+getCookie(name)+';expires='+(new Date(1))+';domain='+domain+';path=/';
34     }

 比如现在我要删除_uij这个cookie,我从chrome开发者工具,application面板cookie选项查看以下该cookie的domain为.renren.com(注意renren前面有个.)

delCookie('_uij', '.renren.com');

就可以删除了

转载于:https://www.cnblogs.com/cococe/p/10475769.html

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用JS删除cookie,你可以使用以下代码作为参考: ``` $("#u2").click(function () { // 删除cookie var exp = new Date(); exp.setTime(exp.getTime() - 1); document.cookie = "id=0; expires=" + exp.toGMTString() + "; path=/"; document.cookie = "username=0; expires=" + exp.toGMTString() + "; path=/"; document.cookie = "password=0; expires=" + exp.toGMTString() + "; path=/"; document.cookie = "nickname=0; expires=" + exp.toGMTString() + "; path=/"; $("#lt").text("未登录"); $("button").css({"color":"white"}); $("#u2").css({"display":"none"}); alert("退登成功"); }); ``` 这段代码使用了`document.cookie`属性来删除cookie。首先,创建一个`Date`对象 `exp`,并将其设置为当前时间减去1天。然后,通过给cookie设置过期时间为`exp.toGMTString()`将其删除。在此示例中,它删除了名为"id"、"username"、"password"和"nickname"的cookie,并重置了相关的元素和文本。 希望这对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [JS设置,获取,删除cookie](https://blog.csdn.net/weixin_44476410/article/details/123137876)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [JS设置cookie删除cookie](https://blog.csdn.net/qq_39704803/article/details/82782310)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值