设置 cookie 会导致意外行为
我有一个多语言网站,可以在德语和英语之间切换。我想将选择的语言存储在 cookie 中,以便访问者下次访问该网站时不必一次又一次地将其切换到他喜欢的语言。不幸的是我对此有一种奇怪的行为。它看起来像这样:
我有一个用于切换语言的链接(onclick)。这个函数看起来像这样:
function storeLanguage(lang) {
/*deletes the cookie? */
document.cookie = "MYCOOKIE=; expires=Thu, 01-Jan-70 00:00:01 GMT;";
var ablauf = new Date();
var expTime = ablauf.getTime() + (60 * 24 * 60 * 60 * 1000); //Cookie for 60 days
ablauf.setTime(expTime);
if (lang == 'en') {
document.cookie = "MYCOOKIE=EN; expires=" + ablauf.toGMTString() + ";";
}
else {
document.cookie = "MYCOOKIE=DE; expires=" + ablauf.toGMTString() + ";";
}
}
Firebug说它跳转到右边的IF分支,所以当我点击“德语”时,这部分将执行“MYCOOKIE=DE”,否则执行英语分支。这看起来是对的。但是,当我访问我网站的任何网页(为了测试,我选择了索引站点)并执行时,
alert(document.cookie);
我突然在德语网页“MYCOOKIE=EN”上得到结果,尽管它应该是“MYCOOKIE=DE”,因为函数跳转到右 IF 分支(else 分支)。当我切换到英语时,我在英语网页上得到结果“MYCOOKIE=DE; MYCOOKIE=EN”。所以突然有两个同名的饼干。所以cookies的值不仅被颠倒了,也没有被正确删除,而是完全是垃圾。谁能解释一下我的代码中有什么问题导致了这种行为?
I have a multilingual website with the possibility to switch between German and English. I want to store the chosen language in a cookie so that the visitor don't have to switch it over and over again to his prefered language when he visits the website next time. Unfortunately I have a strange behaviour with that. It looks like this:
I have a link for switching the language (onclick). This function looks like this:
function storeLanguage(lang) {
/*deletes the cookie? */
document.cookie = "MYCOOKIE=; expires=Thu, 01-Jan-70 00:00:01 GMT;";
var ablauf = new Date();
var expTime = ablauf.getTime() + (60 * 24 * 60 * 60 * 1000); //Cookie for 60 days
ablauf.setTime(expTime);
if (lang == 'en') {
document.cookie = "MYCOOKIE=EN; expires=" + ablauf.toGMTString() + ";";
}
else {
document.cookie = "MYCOOKIE=DE; expires=" + ablauf.toGMTString() + ";";
}
}
Firebug says that it jumps into the right IF-branch, so when I click "German", this part will be executed "MYCOOKIE=DE", otherwise the english branch. This looks right. But when I get to any web page of my website (for test I chose the index site) and execute an
alert(document.cookie);
I suddendly get as result at the German web page "MYCOOKIE=EN", although it should be "MYCOOKIE=DE" because the function jumped into the right IF-branch (else branch). When I switch to English language, I get as result on the english web page "MYCOOKIE=DE; MYCOOKIE=EN". So suddendly there are two cookies with the same name. So the values of the cookies are not just inverted, and not deleted right, but totally crap. Can anybody explain what's wrong in my code which leads to this behaviour?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保您也正确设置了 cookie 的路径。例如,如果您网站的语言嵌入在 URL 中(IE:http://yoursite.com/en /index.html),您可以将 cookie 设置为仅在路径包含“/en”时才有效。完整的 cookie 设置语法如下所示:
Make sure that you are properly setting the path of the cookie as well. For example if the language of your site is embedded in the URL (IE: http://yoursite.com/en/index.html), you may be setting a cookie to only be valid if the path contains "/en". Full cookie setting syntax looks like this: