gpt4 book ai didi

javascript - 添加cookie过期日期

转载 作者:行者123 更新时间:2023-12-02 23:33:55 24 4
gpt4 key购买 nike

我在 SOF 上找到了这个 javascript cookie 脚本,它完全符合我的要求。将首次访问者重定向到不同的 URL。我只希望它的有效期为 1 天。

if (getCookie("first_visit") != "true") {
document.cookie = "first_visit=true";
location.href="http://pgklavertjevier.nl";
}

function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i = 0; i <ca.length; i++) {
var c = ca[i];
while (c.charAt(0)==' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length,c.length);
}
}
return "";
}

最佳答案

您可以在创建 cookie 时设置 cookie 的到期日期(在您的情况下为 1 天)及其值。

如果您通过 JavaScript 创建 cookie,则可以使用以下代码:

function setCookie(name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString()) + ";path=/";
document.cookie = name + "=" + c_value;
}

setCookie("first_visit", true, 1);

关于javascript - 添加cookie过期日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56367871/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com