Javascript检测语言并重定向

时间:2017-06-07 03:22:44

标签: javascript

我知道这里有很多答案。我已经尝试了以下代码,但它保持不工作或保持重定向显示空白页。 我只想检测所有使用中文的人去中文页面,其他人去英文页面。

我应该如何以简单的方式成功完成此功能?

我应该将检测代码放在chi和eng页面中吗?如果网址包含" utm",如何在重定向后将此网址与网址保持一致?

http://www.testing.com/tc/testing.html?utm_source=test

<script type="text/javascript">
       var userLang = navigator.language || navigator.userLanguage;
switch(userLang){
    case 'zh-CN':
        window.location.href = '/tc/testing.html;
        break;
    case 'zh-TW':
        window.location.href = '/tc/testing.html';
        break;
    case 'zh-HK':
        window.location.href = '/tc/testing.html';
        break;
    default:
        // if it's something else fall back to the default
        window.location.href = '/tc/testing.html';
        break;
}
    </script>

<script type='text/javascript'>
var language = window.navigator.language;
if(language == 'zh-CN' || language == 'zh-TW' || language == 'zh-HK')
{
window.location.href = '../tc/testing.html'
}

else {
window.location.href = '../en/testing.html'
}

</script>

0 个答案:

没有答案
相关问题