我希望你能帮我解决我的问题。我的静态 html 网站上有 3 种语言。我想根据用户的浏览器语言设置重定向用户。
这是我的代码:
var lang = navigator.language;
if (lang == 'pl'){
document.location.href = 'index_pl.html';
}
else if (lang == 'fr'){
document.location.href = 'index_fr.html';
}
else {
document.location.href = 'index.html';
}
alert(lang);
问题是每次用户进入网站时,此脚本都会不断刷新/重定向网站。我的问题是如何检查用户浏览器一次,然后将用户重定向到专用网页。
提前致谢。
最佳答案
如果您不希望它继续重定向,您需要检查当前 URL 中的页面。例如:
var lang = navigator.language;
var redirectURL = 'index.html';
if (lang == 'pl'){
redirectURL = 'index_pl.html';
} else if (lang == 'fr'){
redirectURL = 'index.html';
}
if (document.location.pathname.indexOf(redirectURL) == -1) {
document.location.href = redirectURL;
}
这会检查 redirectURL 是否不在路径中。
关于javascript - 静态多语言 javascript 重定向不断重新加载页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40666429/