【JavaScript相关】如何删除一个cookie(约126字)

如何删除一个cookie

删除一个 Cookie 可以通过以下几种方式实现:

1. 将 Cookie 的过期时间设置为过去的时间:

    var date = new Date();
    date.setDate(date.getDate() - 1);
    document.cookie = "cookieName=; expires=" + date.toUTCString();

cookieName 替换为要删除的 Cookie 的名称。

2. 使用expires 参数设置过期时间:

    document.cookie = "cookieName=; expires=Thu, 01 Jan 1970 00:00:00 UTC";

同样,将 cookieName 替换为要删除的 Cookie 的名称。

3. 使用max-age 参数设置过期时间:

    document.cookie = "cookieName=; max-age=0";

同样,将 cookieName 替换为要删除的 Cookie 的名称。

请注意,删除 Cookie 时需要确保 pathdomain 参数与要删除的 Cookie 的设置一致,以确保正确删除指定的 Cookie

THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容