简体   繁体   English

如果在检测语言重定向页面期间语句不起作用

[英]If statement isn't working during detecting the language to redirect the page

Code skips the if statement. 代码跳过if语句。

I've searched for a while now for the answer but couldn't find a solution, and since the similar questions are marked as duplicate I wanted to say these first. 我已经搜索了一段时间以寻找答案,但是找不到解决方案,并且由于类似的问题被标记为重复,因此我想先说这些。 So here is the code: 所以这是代码:

    <script type="text/javascript">
    function loadDocC1() {
    var userLang = navigator.language || navigator.userLanguage; 
    var lang = ("en-US");
    if (lang=userLang){
    window.location.replace("http://karabatak.info");
    }else {window.location.replace("http://kontrabandapart.info");}
    }
    </script>

code chooses one direction to go (either one site or other) regardless of variables. 无论变量如何,代码都会选择一个方向(一个站点或另一个站点)。

here is the error 这是错误

if (lang=userLang){

should be 应该

if (lang == userLang){

Have a look at this 看看这个

Js operators JS运算符

The problem is in your if statement: 问题出在您的if语句中:

if (lang = userLang)

You're using the assignment operator ( = ) here - this assigns a value to a variable. 您在这里使用赋值运算符= )-这将为变量分配一个值。 You want to use the equality / comparison operator ( == ) - this tests whether one value is equal to another. 您想使用相等/比较运算符== )-这将测试一个值是否等于另一个值。 Change your if statement to this: 将您的if语句更改为此:

if (lang == userLang)

And your code will work. 并且您的代码将起作用。

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

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