js设置获取删除cookie

 function setCookie(name,value)  
    {  
        var Days = 30;  
        var exp = new Date();  
        exp.setTime(exp.getTime() + Days*24*60*60*1000);  
        document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();  
        var strsec = getsec(time);  
        var exp = new Date();  
        exp.setTime(exp.getTime() + strsec*1);  
        document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();  
    }  
    //读取cookies  
    function getCookie(name)  
    {  
        var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");  
        if(arr=document.cookie.match(reg))  
            return (unescape(arr[2]));  
        else  
            return null;  
    }  
    //删除cookies  
    function delCookie(name)  
    {  
        var exp = new Date();  
        exp.setTime(exp.getTime() - 1);  
        var cval=getCookie(name);  
        if(cval!=null)  
            document.cookie= name + "="+cval+";expires="+exp.toGMTString();  
    }  
  • 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、付费专栏及课程。

余额充值