删除 Cookie

发布于 2024-11-27 04:20:24 字数 1221 浏览 0 评论 0原文

我正在为我的网站创建一个添加 [ITEM] 功能,其中将包含 cookie。 现在添加 [ITEM] 部分可以工作,但我需要删除 [ITEM]。 这是我的代码:

$(window).load(function(){
   function getCookie(c_name){
 var i,x,y,ARRcookies=document.cookie.split(";");
 for (i=0;i<ARRcookies.length;i++){
   x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
   y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
   x=x.replace(/^\s+|\s+$/g,"");
   if (x==c_name){
     return unescape(y);
     }}}
 function setCookie(c_name,value,exdays){
 var exdate=new Date();
 exdate.setDate(exdate.getDate() + exdays);
 var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
 document.cookie=c_name + "=" + c_value;
 }
     $(document).ready(function(){
var cookieyess=getCookie("save");
 if(cookieyess!==undefined&&cookieyess!==null&&cookieyess!==""){
$('#save1').show();
    }
    else{
 $('#save1').hide();
   }
 }); 
  $('#save_1').click(function(){
 var cookieyes=getCookie("save");
 if(cookieyes!==undefined&&cookieyes!==null&&cookieyes!==""){
$('#save1').show();
    }
    else{
 $('#save1').show();
        setCookie("save","yes",365);
    }
  });
});

现在我需要创建一个函数,以便当您单击“删除”时它会删除该 Cookie。

I'm creating an add [ITEM] feature to my site that will include cookies.
Now the Add [ITEM] part works but I need an Remove [ITEM].
This is my code:

$(window).load(function(){
   function getCookie(c_name){
 var i,x,y,ARRcookies=document.cookie.split(";");
 for (i=0;i<ARRcookies.length;i++){
   x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
   y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
   x=x.replace(/^\s+|\s+$/g,"");
   if (x==c_name){
     return unescape(y);
     }}}
 function setCookie(c_name,value,exdays){
 var exdate=new Date();
 exdate.setDate(exdate.getDate() + exdays);
 var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
 document.cookie=c_name + "=" + c_value;
 }
     $(document).ready(function(){
var cookieyess=getCookie("save");
 if(cookieyess!==undefined&&cookieyess!==null&&cookieyess!==""){
$('#save1').show();
    }
    else{
 $('#save1').hide();
   }
 }); 
  $('#save_1').click(function(){
 var cookieyes=getCookie("save");
 if(cookieyes!==undefined&&cookieyes!==null&&cookieyes!==""){
$('#save1').show();
    }
    else{
 $('#save1').show();
        setCookie("save","yes",365);
    }
  });
});

Now I need to make a function so when you click Removes it deletes that Cookie.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

此刻的回忆 2024-12-04 04:20:24

要删除 mycookie,这应该通过设置过去的到期日期来实现:

setCookie("mycookie", "", -1)

To delete mycookie, this should work by setting an expiration date in the past:

setCookie("mycookie", "", -1)
过潦 2024-12-04 04:20:24

这是我写的一个小库:

var myCookieHandler = (function () {
    return {
        createCookie: function (name, value, days) {
            var expires = "";
            if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                expires = "; expires=" + date.toGMTString();
            }
            document.cookie = name + "=" + value + expires + "; path=/";
        },
        readCookie: function (name) {
            var nameEq = name + "=";
            var ca = document.cookie.split(';');
            var i;

            for (i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) === ' ') { c = c.substring(1, c.length); }
                if (c.indexOf(nameEq) === 0) { return c.substring(nameEq.length, c.length); }
            }

            return null;
        },
        deleteCookie: function (name) {
            this.createCookie(name, null, -1);
        }
    };
}());

用法:

myCookieHandler .writeCookie("Login","true",2);

var cookieValue=myCookieHandler.readCookie("Login");

myCookieHandler.deleteCookie("Login");

希望这会有所帮助..

Here is a small library that I wrote:

var myCookieHandler = (function () {
    return {
        createCookie: function (name, value, days) {
            var expires = "";
            if (days) {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                expires = "; expires=" + date.toGMTString();
            }
            document.cookie = name + "=" + value + expires + "; path=/";
        },
        readCookie: function (name) {
            var nameEq = name + "=";
            var ca = document.cookie.split(';');
            var i;

            for (i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) === ' ') { c = c.substring(1, c.length); }
                if (c.indexOf(nameEq) === 0) { return c.substring(nameEq.length, c.length); }
            }

            return null;
        },
        deleteCookie: function (name) {
            this.createCookie(name, null, -1);
        }
    };
}());

usage:

myCookieHandler .writeCookie("Login","true",2);

var cookieValue=myCookieHandler.readCookie("Login");

myCookieHandler.deleteCookie("Login");

Hope this helps..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文