问题描述
我需要删除具有相同名称但路径不同的客户端Cookie。
只需指定要删除的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
更改它以使路径为 /
仍然只会过期一个Cookie - 给定的路径必须与设置的路径匹配:
document.cookie ='name =; path = /; expires ='+ new Date(0).toUTCString();
alert(document.cookie); // name = value2
要删除它们,必须使用以下路径到期: / p>
document.cookie ='name =; path = /; expires ='+ new Date(0).toUTCString();
document.cookie ='name =; path = / path /; expires ='+ new Date(0).toUTCString();
alert(document.cookie); // {blank}
现在,这些示例假设您正在浏览 /路径/
或其子目录。
要批量删除,请尝试这样:
function expireAllCookies(name,路径){
var expires = new Date(0).toUTCString();
//过期空路径cookie以及
document.cookie = name +'=; expires ='+ expires;
for(var i = 0,l = paths.length; i document.cookie = name +'=; path ='+ paths [i] +'; expires ='+ expires;
}
}
expireAllCookies('name',['/','/ path /']);
演示:
您还可以通过分割和迭代来伪装路径查找 window.location.pathname
:
function expireActiveCookies(name){
var pathname = location.pathname.replace \ / $ /,''),
segments = pathname.split('/'),
paths = [];
for(var i = 0,l = segments.length,path; i path = segments.slice(0,i + 1).join '/');
paths.push(path); // as file
paths.push(path +'/'); // as directory
}
expireAllCookies(name,paths);
}
演示:
I need to delete clientside cookies with the same name but with different paths. What is the best way to do this in javascript.
Just specify the same path of the cookie you want to remove, giving it a past expiration.
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:
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.
[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/
You can also fake path lookups by splitting and iterating 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);
}
Demo: http://jsfiddle.net/M2dZ3/2/
这篇关于删除名称相同但路径不同的Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!