javascript 删除同名但路径不同的cookies
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7487210/
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
Removing cookies with the same name but different paths
提问by JKown
I need to delete clientside cookies with the same name but with different paths. What is the best way to do this in javascript.
我需要删除具有相同名称但具有不同路径的客户端 cookie。在 javascript 中执行此操作的最佳方法是什么。
回答by Jonathan Lonowski
Just specify the same path of the cookie you want to remove, giving it a past expiration.
只需指定要删除的 cookie 的相同路径,使其过期。
document.cookie = 'name=value1; path=/';
document.cookie = 'name=value2; path=/path/';
alert(document.cookie); // name=value1; name=value2
document.cookie = 'name=; path=/path/; expires=' + new Date(0).toUTCString();
alert(document.cookie); // name=value1
Changing it to expire the cookie with a path of /
will still only expire one of the cookies -- the path given has to match the path set:
将其更改为使路径为 的 cookie/
仍然只会使其中一个 cookie 过期——给定的路径必须与路径集相匹配:
document.cookie = 'name=; path=/; expires=' + new Date(0).toUTCString();
alert(document.cookie); // name=value2
To remove both, you'll have to expire each with their path:
要删除两者,您必须使用它们的路径使每个过期:
document.cookie = 'name=; path=/; expires=' + new Date(0).toUTCString();
document.cookie = 'name=; path=/path/; expires=' + new Date(0).toUTCString();
alert(document.cookie); // {blank}
Now, these examples assume you're browsing /path/
or a sub-directory of it.
现在,这些示例假设您正在浏览它/path/
或其子目录。
[edit]
[编辑]
To remove in bulk, try something like this:
要批量删除,请尝试以下操作:
function expireAllCookies(name, paths) {
var expires = new Date(0).toUTCString();
// expire null-path cookies as well
document.cookie = name + '=; expires=' + expires;
for (var i = 0, l = paths.length; i < l; i++) {
document.cookie = name + '=; path=' + paths[i] + '; expires=' + expires;
}
}
expireAllCookies('name', ['/', '/path/']);
Demo: http://jsfiddle.net/M2dZ3/
演示:http: //jsfiddle.net/M2dZ3/
You can also fake path lookups by splitting and iterating window.location.pathname
:
您还可以通过拆分和迭代来伪造路径查找window.location.pathname
:
function expireActiveCookies(name) {
var pathname = location.pathname.replace(/\/$/, ''),
segments = pathname.split('/'),
paths = [];
for (var i = 0, l = segments.length, path; i < l; i++) {
path = segments.slice(0, i + 1).join('/');
paths.push(path); // as file
paths.push(path + '/'); // as directory
}
expireAllCookies(name, paths);
}
回答by JMax
You can set a cookie with the same parameters but a date in the past:
您可以设置具有相同参数但日期为过去的 cookie:
document.cookie =
'ppkcookie2=another test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'
See some valuable information on quirksmode
查看有关 quirksmode 的一些有价值的信息
[EDIT] To delete the cookies with the same name, you can try:
[编辑] 要删除同名的 cookie,您可以尝试:
function delete_cookie ( cookie_name )
{
var cookie_date = new Date ( ); // current date & time
cookie_date.setTime ( cookie_date.getTime() - 1 );
document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}