js设置 获取 删除cookie

function setCookie(key,value) {
        var date = new Date(),
                t = 5;
        date.setDate( date.getDate() + t );
        document.cookie = key+'='+encodeURIComponent(value)+';expires='+date.toGMTString();
    }
    // setCookie('username','liuwei');
    // setCookie('password','000000');
    // console.log(document.cookie);    //username=liuwei; password=000000
    // function getCookie(key) {
    //     var arr = document.cookie.split('; ');
    //     for (var i = 0; i < arr.length; i++) {
    //         var arr2 = arr[i].split('=');
    //         for (var j = 0; j < arr2.length; j++) {
    //             if (arr2[0] == key) {
    //                 return arr2[1];
    //             }
    //         };
    //     };
    // }
    function getCookie(key) {
        var arr,reg = RegExp('(^| )'+key+'=([^;]+)(;|$)');
        if (arr = document.cookie.match(reg))    //["username=liuwei;", "", "liuwei", ";"]
            return decodeURIComponent(arr[2]);
        else
            return null;
    }

    function delCookie(key) {
        var date = new Date();
        date.setTime(date.getTime() - 1);
        var delValue = getCookie(key);
        if (!!delValue) {
            document.cookie = key+'='+delValue+';expires='+date.toGMTString();
        }
    }
    delCookie('username');

    // console.log(document.cookie);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值