gpt4 book ai didi

Javascript - 设置浏览器 Cookie

转载 作者:行者123 更新时间:2023-11-28 03:08:45 24 4
gpt4 key购买 nike

当用户单击按钮时,他们会转到一个链接,但在转到该链接之前,需要将 cookie 设置为英语 (EN) 或法语 (FR)。我这里有一个例子: http://www.quirksmode.org/js/cookies.html

但由于某种原因,它没有读取 cookie,我不确定我哪里出错了。这就是我所拥有的:

<!DOCTYPE html>
<html>
<body>

<p>This is for the Browser Cookies.</p>

<button onclick="EN_Cookie()">English Link</button> <button onclick="FR_Cookie()">French Link</button>


<script>
function EN_Cookie() {
setCookie("SMDCookie","EN",7);
//location.href = 'https://google.com';
}

function FR_Cookie() {
setCookie("SMDCookie","FR",7);
//location.href = 'https://google.com';
}

function setCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}

</script>

</body>
</html>

有什么建议吗?

最佳答案

我查看了您的代码,发现您设置的 cookie 函数是正确的。可能是你的getCookie不起作用,我分享一下获取cookie的功能:

  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 "";
}

关于Javascript - 设置浏览器 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60356303/

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