双语言浏览器重定向 Javascript [英] Dual language browsers redirect Javascript

查看:42
本文介绍了双语言浏览器重定向 Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将英语浏览器重定向到 xyz.com/?lang=en,同时让瑞典浏览器留在 xyz.com

I am trying to redirect English browsers to xyz.com/?lang=en while letting Swedish ones stay on xyz.com

我一直在尝试:

var type=navigator.appName
if (type=="Netscape")
var lang = navigator.language
else
var lang = navigator.userLanguage

//cut down to first 2 chars of country code
var lang = lang.substr(0,2)

// Swedish
if (lang == "sv")
window.location.replace('????')

// if none of above (default to English or any other)
else
window.location.replace('xyz.com/?lang=en')
</script>

但我不知道如何编写瑞典语 URL,因为它不是重定向,因为默认语言是瑞典语...编写 xyz.com 让我进入重定向循环

But I don't know how to write the Swedish URL since it's not a redirection as the default language is swedish... writing the xyz.com gets me into a redirection loop

if ($_REQUEST["lang"] == "en") 
{ 
    echo '<div class="langlight"><a href="http://xyz.com/">Svenska</a></div>';
} 
else 
{
    echo '<div class="langbold"><a href="http://xyz.com/">Svenska</a></div>';

}

if ($_REQUEST["lang"] == "en") 
{
    echo '<div class="langbold"><a href="http://xyz.com/">English</a></div>';
} 
else 
{
    echo '<div class="langlight"><a href="xyz.com/">English</a></div>';
}

    enter code here

推荐答案

if (lang !== "sv") {
    window.location.replace(window.location.href + '?lang=en');
}

这篇关于双语言浏览器重定向 Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆