【问题标题】:How to stop execution of javascript if condition is true?如果条件为真,如何停止执行javascript?
【发布时间】:2016-12-15 06:38:55
【问题描述】:

我想用 http://www.example.com.index.html?source=mobile 之类的特定 URL 标记我的移动访问者,我使用此脚本将访问者重定向到给定的 url

<script type="text/javascript">
<!--
if (screen.width <= 800) 
{
    window.location ="http://www.example.com/index.html?source=mobile";
}

//-->
</script>

但是当我使用此脚本时,页面会一次又一次地加载,因此我尝试在此脚本中添加另一个条件,但我很困惑如何使第二个条件一次又一次地停止加载。

<script type="text/javascript">
<!--
if (screen.width >= 800) 
{
    window.location ="http://www.example.com/index.html?source=mobile";
}
 Else if (document.referrer ="http://www.example.com/index.html?source=mobile")
{
    //stop execution here
}


//-->
</script>

如果第二个条件为真,现在请有人帮我停止执行 javascript。

提前致谢。

【问题讨论】:

  • 只使用return语句。
  • 永远不会满足第二个条件,因为第一个条件已经重定向,请参阅下面的答案。谢谢
  • 我刚刚在下面给你写了一个 JS 代码,我并不是说这是你能得到的完美但它可能会工作 - 我已经在我的 Chrome JS 控制台中对其进行了测试。

标签: javascript


【解决方案1】:

将语句包装在匿名函数中:

(function(){
    if (screen.width >= 800)  {
        window.location = "http://www.example.com/index.html?source=mobile";
    }else if (document.referrer == "http://www.example.com/index.html?source=mobile") {
        return;
    }
});

尽管其他答案声称,您只能在函数内使用 return。

我还假设您将在 if 语句下添加更多代码,否则停止执行是没有用的。

您的代码存在一些问题:

  1. 你用大写 E 拼写 else 并且 is 应该是小写
  2. 在 else if 语句中您没有使用两个等号

以上代码修改了两个问题

【讨论】:

  • return false 会更方便
【解决方案2】:

怎么样

if (screen.width >= 800 && document.referrer !="http://www.example.com/index.html?source=mobile") 
{
    window.location ="http://www.example.com/index.html?source=mobile";
}

你现在设置它的方式不会像第一个 if 总是被满足那样工作,你应该按照我在这里的建议去做,或者把文档引用条件放在第一位(另外你需要 == 而不是 =)。

【讨论】:

  • @lededje 我解释了为什么尝试终止脚本并不能解决他不断重新加载页面的问题,而这解决了这个问题,那么您的观点到底是什么?
  • 这个答案是完全相关的,但这不是一个很好的解决方案。我试过了,但似乎 Google Chrome Javascript 引擎会继续执行,直到从 http 通道返回新的页面数据。
【解决方案3】:

你应该直接在 php/whateveryouuse 中检测移动浏览器,有很多移动检测脚本(检查用户代理how to check if the request came from mobile or computer in php)

现在的情况:用户进入网站,如果分辨率低,他会被重定向到……同一个页面! (加载网站两次)

如果您检测到移动服务器端:用户只进入网站一次

另外,如果您只关心浏览器的分辨率,您应该阅读一些有关响应式设计的内容

【讨论】:

    【解决方案4】:

    使用window.location.href 获取网址

    你有一个错字:else if.. 不是Else if

     if (screen.width >= 800 && window.location.href !="http://www.example.com/index.html?source=mobile") 
    {
        window.location.href ="http://www.example.com/index.html?source=mobile";
    }
    

    在停止执行的情况下,

    else if (window.location.href ="http://www.example.com/index.html?source=mobile")
            {
                return;//to stop execution here..use return;
            }
       // This condition is unnecessary though
    

    注意:return true,return false and return有区别

    return false:阻止事件“冒泡”。暂停执行

    return true:继续执行。

    返回;

    这里引用了 JavaScript 所基于的 ECMA 262 标准关于返回的内容;如前所述,结果是“未定义”。

    如果 ECMAScript 程序在语法上不正确,则 包含不在 FunctionBody 中的 return 语句。一个 return 语句导致函数停止执行并返回一个 对调用者的价值。如果省略表达式,则返回值为 不明确的。否则返回值为 Expression 的值。

    【讨论】:

    • 大写E in else? SyntaxError: Unexpected token if。 return 不在函数中? SyntaxError: Illegal return statement
    • 另外,在if 中设置document.referrer? (我知道这是copypasta)
    【解决方案5】:

    IE8、IE8+、IE11、Firefox、Chrome:

    <script>
      ... I wana cancel here ...
      if (document.execCommand) document.execCommand('Stop');   
      if (window.stop) window.stop();   
    </script>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-14
      • 2023-03-12
      • 1970-01-01
      • 2012-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多