删除同名但不同路径的cookie

发布于 2024-12-05 16:23:30 字数 64 浏览 1 评论 0原文

我需要删除具有相同名称但具有不同路径的客户端cookie。在 javascript 中执行此操作的最佳方法是什么?

I need to delete clientside cookies with the same name but with different paths. What is the best way to do this in javascript.

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

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

发布评论

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

评论(1

归属感 2024-12-12 16:23:30

只需指定您要删除的 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 过期,仍然只会使其中一个 cookie 过期 - 给定的路径必须与设置的路径匹配:

document.cookie = 'name=; path=/; expires=' + new Date(0).toUTCString();

alert(document.cookie); // name=value2

要删除两者,您必须使每个 cookie 过期他们的路径:

document.cookie = 'name=; path=/; expires=' + new Date(0).toUTCString();
document.cookie = 'name=; path=/path/; expires=' + new Date(0).toUTCString();

alert(document.cookie); // {blank}

现在,这些示例假设您正在浏览 /path/ 或其子目录。


[编辑]

要批量删除,请尝试以下操作:

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/']);

演示: http://jsfiddle.net/M2dZ3/

您还可以通过拆分和迭代 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);
}

演示:http://jsfiddle.net/M2dZ3/2/

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/

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