我有一个索引站点,它检查 cookie 是否存在以及它是否具有 value EN
。如果是这样,它应该重定向到 English index.shtml
。否则,或者如果没有(英语)cookie,它应该重定向到德语下一页:
if (document.cookie) {
var cookieValue = document.cookie;
if (cookieValue.indexOf("MYCOOKIE=EN") > -1) {
window.location.href="en/index.shtml";
}
}
window.location.href="kategorien/hauptkategorie.shtml";
现在发生了一些非常奇怪的事情:英文cookie存在(我检查cookieValue
了JavaScript警报并显示EN
),但即使if
不会执行内部的href,但会执行第二个href。为什么会这样?
当我添加 2 时else
,它的工作方式与预期一样:
if (document.cookie) {
var cookieValue = document.cookie;
if (cookieValue.indexOf("MYCOOKIE=EN") > -1) {
window.location.href="en/index.shtml";
}
else {
window.location.href="kategorien/hauptkategorie.shtml";
}
}
else {
window.location.href="kategorien/hauptkategorie.shtml";
}
hauptkategorie.shtml
为什么如果我离开s ,它会重定向到else
?