根据浏览器语言的 Javascript 重定向
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24519370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript redirection according to browser language
提问by KUMO
I'm trying to send visitors that have browser language in English to an alternative site. I was able to find this code but it's not working:
我正在尝试将浏览器语言为英语的访问者发送到其他站点。我能够找到此代码,但它不起作用:
<script type="type/javascript">
var language = navigator.browserLanguage;
// alert(language);
if (language.indexOf('en') > -1) {
document.location.href = 'http://en.socialpos.com.ar';
} else {
document.location.href = 'http://socialpos.com.ar';
}
</script>
I'm not even getting the alert :/
You can see it in http://socialpos.com.ar
我什至没有收到警报:/
您可以在http://socialpos.com.ar 中看到它
回答by Sterling Archer
var language = navigator.browserLanguage;
should be
应该
var language = navigator.language || navigator.browserLanguage; //for IE
see my console results:
查看我的控制台结果:
var language = navigator.browserLanguage;
undefined
language;
undefined
var language = navigator.language;
undefined
language;
"en-US"
Also please note that this was the first result for a google search: "javascript browser language". Google is your friend, and your google-fu is weak. Train it with searches!
另请注意,这是谷歌搜索的第一个结果:“javascript 浏览器语言”。谷歌是你的朋友,你的谷歌功能很弱。通过搜索训练它!
回答by zwacky
assuming your alert(language)
wasn't commented out, your script tag should be
假设你alert(language)
没有被注释掉,你的脚本标签应该是
<script type="text/javascript"></script>
or
或者
<script></script>