js获取浏览器语言

2019-08-02 15:48:05 浏览数 (1)

!function () { var lang = navigator.language||navigator.userLanguage;//常规浏览器语言和IE浏览器 lang = lang.substr(0, 2);//截取lang前2位字符 if(lang == 'zh'){ window.location.replace('./cn/home/index.html'); }else{ window.location.replace('./en/home/index.html') } }()

<script type="text/javascript">   var type = navigator.appName;   if (type == "Netscape"){   var lang = navigator.language;//获取浏览器配置语言,支持非IE浏览器   }else{   var lang = navigator.userLanguage;//获取浏览器配置语言,支持IE5 == navigator.systemLanguage   };   var lang = lang.substr(0, 2);//获取浏览器配置语言前两位   if (lang == "zh"){    alert(lang);    //window.location.replace('url');//中文编码时打开链接   }else if (lang == "en"){    window.location.replace('url');   }else{//其他语言编码时打开以下链接    window.location.replace('url');   }; </script>

0 人点赞