简体   繁体   English

语言重定向

[英]Language redirection

I'm new to programming. 我是编程新手。 Decided to create and launch my first website. 决定创建并推出我的第一个网站。 It consists only of 2 html pages. 它只包含2个html页面。 Page one is in Russian (default) and page 2 is in English. 第一页是俄语(默认),第二页是英语。

I want JS to redirect to the English page all the users who have their browsers languages not in Russian. 我希望JS将所有浏览器语言不是俄语的用户重定向到英文页面。

So I tried to use this code. 所以我试着使用这段代码。

<script type="text/javascript">
    $( document ).ready(function(){
        var userLang = navigator.language || navigator.userLanguage;
        if (userLang == "ru") {
            break;
        }
        else {
            window.location.href = "/en/index.html"
        }
    });
</script>

But nothing happened. 但什么都没发生。

Your break statement is invalid there. 你的break声明在那里无效。 Also, what you're trying to achieve doesn't need an else statement - you can redirect user to the english version if the language is not russian. 此外,您正在尝试实现的不需要else语句 - if语言不是俄语,您可以将用户重定向到英语版本。

 <script type="text/javascript">
        $( document ).ready(function(){
            var userLang = navigator.language || navigator.userLanguage;
            if (userLang !== "ru") {
                window.location.href = "/en/index.html";
            }
        });
    </script>

This is your code after a little tweak. 经过一些调整后,这是您的代码。 As someone already mentioned - you could achieve that easily without jQuery. 正如有人已经提到过的 - 如果没有jQuery,你可以轻松实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM