【发布时间】:2020-06-06 22:12:33
【问题描述】:
当用户点击一个按钮时,它会将他们带到一个链接,但在他们转到该链接之前,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”是什么意思?
-
你在哪里读取cookie?在本地运行
document.cookie.split(';').filter(x => x.includes('SMDCookie'))说明cookie设置功能有效。
标签: javascript html cookies