Javascript 按名称删除cookie?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10593013/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:02:19  来源:igfitidea点击:

Delete cookie by name?

javascriptcookies

提问by Charlie

How can I delete a specific cookie with the name roundcube_sessauth?

如何删除名称为 的特定 cookie roundcube_sessauth

Shouldn't the following:

不应该如下:

function del_cookie(name) {
    document.cookie = 'roundcube_sessauth' + 
    '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
} 

And then:

进而:

<a href="javascript:del_cookie(name);">KILL</a>

Kill the roundcube_sessauthcookie?

杀死roundcube_sessauth饼干?

回答by emii

You should define the path on which the cookie exists to ensure that you are deleting the correct cookie.

您应该定义 cookie 所在的路径,以确保您正在删除正确的 cookie。

function set_cookie(name, value) {
  document.cookie = name +'='+ value +'; Path=/;';
}
function delete_cookie(name) {
  document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

If you don't specify the path, the browser will set a cookie relative to the page you are currently on, so if you delete the cookie while on a different page, the other cookie continues its existence.

如果您不指定路径,浏览器将设置一个相对于您当前所在页面的 cookie,因此如果您在不同页面上删除该 cookie,另一个 cookie 将继续存在。

Edit based on @Evan Morrison's comment.
Be aware that in some cases to identify the correct cookie, the Domainparameter is required.
Usually it's defined as Domain=.yourdomain.com.
Placing a dotin front of your domain name means that this cookie may exist on any sub-domain (wwwalso counts as sub-domain).

根据@Evan Morrison 的评论进行编辑。
请注意,在某些情况下,要识别正确的 cookie,该Domain参数是必需的。
通常它被定义为Domain=.yourdomain.com. 在您的域名前
放置一个表示此 cookie 可能存在于任何子域(www也算作子域)中。

Also, as mentioned in @RobertT's answer, HttpOnlycookies cannot be deleted with JavaScript on the client side.

此外,正如@RobertT 的回答中所述,HttpOnly无法在客户端使用 JavaScript 删除 cookie。

回答by emii

In order to delete a cookie set the expiresdate to something in the past. A function that does this would be.

为了删除cookie,请将expires日期设置为过去。执行此操作的函数将是。

var delete_cookie = function(name) {
    document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};

Then to delete a cookie named roundcube_sessauthjust do.

然后删除一个名为roundcube_sessauthjust do的cookie 。

delete_cookie('roundcube_sessauth');

回答by Kishor Patil

//if passed exMins=0 it will delete as soon as it creates it.

//如果通过 exMins=0 它将在创建它后立即删除。

function setCookie(cname, cvalue, exMins) {
    var d = new Date();
    d.setTime(d.getTime() + (exMins*60*1000));
    var expires = "expires="+d.toUTCString();  
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

setCookie('cookieNameToDelete','',0) // this will delete the cookie.

回答by RobertT

I'm not really sure if that was the situation with Roundcube version from May '12, but for current one the answer is that you can't delete roundcube_sessauthcookie from JavaScript, as it is marked as HttpOnly. And this means it's not accessible from JS client side code and can be removed only by server side script or by direct user action (via some browser mechanics like integrated debugger or some plugin).

我不确定 12 年 5 月的 Roundcube 版本是否就是这种情况,但对于当前版本,答案是您无法roundcube_sessauth从 JavaScript 中删除cookie,因为它被标记为HttpOnly. 这意味着它不能从 JS 客户端代码访问,只能通过服务器端脚本或直接用户操作(通过一些浏览器机制,如集成调试器或某些插件)删除。

回答by Nafees

You can try this solution

你可以试试这个解决方案

var d = new Date();
d.setTime(d.getTime());
var expires = "expires="+d.toUTCString();
document.cookie = 'COOKIE_NAME' + "=" + "" + ";domain=domain.com;path=/;expires=" + expires;

回答by Amir Movahedi

In my case I used blow code for different environment.

就我而言,我在不同的环境中使用了打击代码。

  document.cookie = name +`=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;Domain=.${document.domain.split('.').splice(1).join('.')}`;