Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何删除cookie?_Javascript_Cookies - Fatal编程技术网

Javascript 如何删除cookie?

Javascript 如何删除cookie?,javascript,cookies,Javascript,Cookies,我创建cookie的功能正确吗?如何在程序开始时删除cookie?有简单的编码吗 function createCookie(name,value,days) 这里有一个很好的链接 函数setCookie(名称、值、天数){ var=”; 如果(天){ 变量日期=新日期(); date.setTime(date.getTime()+(天*24*60*60*1000)); expires=“;expires=“+date.toutString(); } document.cookie=name+

我创建cookie的功能正确吗?如何在程序开始时删除cookie?有简单的编码吗

function createCookie(name,value,days)
这里有一个很好的链接

函数setCookie(名称、值、天数){
var=”;
如果(天){
变量日期=新日期();
date.setTime(date.getTime()+(天*24*60*60*1000));
expires=“;expires=“+date.toutString();
}
document.cookie=name+“=”+(值| |“”)+expires+“path=/”;
}
函数getCookie(名称){
变量nameEQ=name+“=”;
var ca=document.cookie.split(“;”);
对于(变量i=0;i
试试这个:

function delete_cookie( name, path, domain ) {
  if( get_cookie( name ) ) {
    document.cookie = name + "=" +
      ((path) ? ";path="+path:"")+
      ((domain)?";domain="+domain:"") +
      ";expires=Thu, 01 Jan 1970 00:00:01 GMT";
  }
}
您可以这样定义
get\u cookie()

函数获取cookie(名称){
return document.cookie.split(“;”).some(c=>{
返回c.trim().startsWith(name+'=');
});
}

您可以通过将到期日期设置为昨天来完成此操作

将其设置为“-1”不起作用。将cookie标记为会话cookie。

这样行吗

function eraseCookie(name) {
    document.cookie = name + '=; Max-Age=0'
}

我知道
Max Age
在创建cookie时会导致cookie成为IE中的会话cookie。不确定删除Cookie时它是如何工作的。

以下是一个功能的实现,该功能由Mozilla提供unicode支持:

function removeItem(sKey, sPath, sDomain) {
    document.cookie = encodeURIComponent(sKey) + 
                  "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + 
                  (sDomain ? "; domain=" + sDomain : "") + 
                  (sPath ? "; path=" + sPath : "");
}

removeItem("cookieName");
如果您使用AngularJs,请尝试(在其下面使用类似的名称):


要删除cookie,我再次将其设置为空值,并在1秒内过期。 具体来说,我总是使用以下一种口味(我倾向于选择第二种):

1.

    function setCookie(key, value, expireDays, expireHours, expireMinutes, expireSeconds) {
        var expireDate = new Date();
        if (expireDays) {
            expireDate.setDate(expireDate.getDate() + expireDays);
        }
        if (expireHours) {
            expireDate.setHours(expireDate.getHours() + expireHours);
        }
        if (expireMinutes) {
            expireDate.setMinutes(expireDate.getMinutes() + expireMinutes);
        }
        if (expireSeconds) {
            expireDate.setSeconds(expireDate.getSeconds() + expireSeconds);
        }
        document.cookie = key +"="+ escape(value) +
            ";domain="+ window.location.hostname +
            ";path=/"+
            ";expires="+expireDate.toUTCString();
    }

    function deleteCookie(name) {
        setCookie(name, "", null , null , null, 1);
    }
用法:

setCookie("reminder", "buyCoffee", null, null, 20);
deleteCookie("reminder");
setCookie({name: "reminder", value: "buyCoffee", minutes: 20});
deleteCookie("reminder");
2

    function setCookie(params) {
        var name            = params.name,
            value           = params.value,
            expireDays      = params.days,
            expireHours     = params.hours,
            expireMinutes   = params.minutes,
            expireSeconds   = params.seconds;

        var expireDate = new Date();
        if (expireDays) {
            expireDate.setDate(expireDate.getDate() + expireDays);
        }
        if (expireHours) {
            expireDate.setHours(expireDate.getHours() + expireHours);
        }
        if (expireMinutes) {
            expireDate.setMinutes(expireDate.getMinutes() + expireMinutes);
        }
        if (expireSeconds) {
            expireDate.setSeconds(expireDate.getSeconds() + expireSeconds);
        }

        document.cookie = name +"="+ escape(value) +
            ";domain="+ window.location.hostname +
            ";path=/"+
            ";expires="+expireDate.toUTCString();
    }

    function deleteCookie(name) {
        setCookie({name: name, value: "", seconds: 1});
    }
用法:

setCookie("reminder", "buyCoffee", null, null, 20);
deleteCookie("reminder");
setCookie({name: "reminder", value: "buyCoffee", minutes: 20});
deleteCookie("reminder");

我在删除一个通过JavaScript制作的cookie时遇到了问题,在我添加了主机后,cookie工作了(将下面的代码向右滚动,以查看
位置.host
)。清除域上的Cookie后,请尝试以下操作以查看结果:

if (document.cookie.length==0)
{
 document.cookie = 'name=example; expires='+new Date((new Date()).valueOf()+1000*60*60*24*15)+'; path=/; domain='+location.host;

 if (document.cookie.length==0) {alert('Cookies disabled');}
 else
 {
  document.cookie = 'name=example; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain='+location.host;

  if (document.cookie.length==0) {alert('Created AND deleted cookie successfully.');}
  else {alert('document.cookies.length = '+document.cookies.length);}
 }
}

如果手动创建cookie,其他一些解决方案可能无法工作

以下是删除cookie的快速方法:

document.cookie = 'COOKIE_NAME=; Max-Age=0; path=/; domain=' + location.host;

如何设置一个函数来检查我的cookie是什么以及它是否过期?get_cookie没有定义该函数的第二个版本不再工作请参见JSFIDLE。@Luca的anwser工作中的方法该如何工作?JavaScript没有内置的
get_cookie()
函数。@MichałPerłakowski我很确定它只是一个占位符/引用,指向您将在其他地方定义的实际函数。如果您在另一个页面上设置cookie并尝试从其他页面中删除它,您的示例将无法运行。集合可以工作,但无法删除它。我最终使用了:并且您必须使用路径:“/”来删除它。这是一个很好的方法,除了。。。只需将过期时间设置为零。这将导致即时到期,不会让任何人感到困惑(“为什么开发人员将到期时间设置为昨天?这是一个错误吗?他们想要一天的寿命吗?”)。编写代码让它更有意义,这样你的生活就不会那么混乱了。这是当今编码中被低估的哲学。。。甚至MDN也建议将到期时间设置为零以删除cookie。注意:如果cookie不起作用,请确保
path
正确。请参阅:应添加域。还值得注意的是,路径和域都需要有正确的值。经过测试,在页面加载之前,即使在WKWebView中也能完美工作。解决方案做得很好。这一个在Firefox中对我不起作用(我没有在其他地方测试它)。可能是因为缺少location.host。卢卡·博里奥尼的回答确实如此<代码>设置cookie(名称“”,null,null,null,1)尽管其他两个函数运行良好。w3schools在上有很好的cookie函数。您可以使用
setCookie('name','value',0)
删除cookie。这个答案对我来说最有效:设置为零,cookie将在浏览器窗口关闭时过期。
document.cookie = 'COOKIE_NAME=; Max-Age=0; path=/; domain=' + location.host;