一旦 Safari 关闭并重新打开,持久 cookie 就会被删除(在 Chrome、Firefox 中工作正常)
我使用以下 JavaScript 代码将带有 ID 的 cookie 在网站访问者的浏览器中存储 60 天。
虽然它在 Chrome、Firefox 等浏览器中完美运行,但在 Safari 中,cookie 在关闭并重新打开浏览器后会消失。 (即使在标准 cookie 配置中,iPhone、Macbook 等也会发生这种情况)
function setCookie() {
const date = new Date;
date.setDate(date.getDate() + 60);
var uid = Date.now().toString(36) + Math.random().toString(36).substring(2);
document.cookie = "ck=" + uid + "; expires=" + date + "; path=/";
}
有人知道为什么会发生这种情况吗?问候!
I'm using the following JavaScript code to store a cookie with a ID in the browser of the website visitor for 60 days.
While it is working perfectly in Chrome, Firefox etc. in Safari the cookie disappears after closing and reopening the browser. (happens with iPhones, Macbooks etc., even in the standard cookie-configuration)
function setCookie() {
const date = new Date;
date.setDate(date.getDate() + 60);
var uid = Date.now().toString(36) + Math.random().toString(36).substring(2);
document.cookie = "ck=" + uid + "; expires=" + date + "; path=/";
}
Does anyone have an idea, why this could be happening? Greetings!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我决定用 php 设置和读取 cookie,现在它可以工作了。无论如何,也许这是更好的选择,因为使用 document.cookie 设置的 cookie 在 Safari 中最多只能持续 7 天,然后才会被自动删除
(根据此概述)
I decided to set and read the cookie with php, now it is working. Maybe that is the better option anyways, since cookies set with document.cookie can only last maximum 7 days in Safari, before they get automatically deleted
(according to this overview)