如果条件为真,如何停止执行 javascript?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18032842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:32:54  来源:igfitidea点击:

How to stop execution of javascript if condition is true?

javascript

提问by user2648537

I want to tag my mobile visitor with a specific URL like http://www.example.com.index.html?source=mobilei used this script to redirect visitors to the given url

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

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

//-->
</script>

But when i used this script the page kept on loading again and again so i tried to add another condition in this script but i am confused how to make the second condition to stop loading again and again.

但是当我使用这个脚本时,页面不断加载,所以我试图在这个脚本中添加另一个条件,但我很困惑如何使第二个条件一次又一次地停止加载。

<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>

Now plz someone help me to stop execution of javascript if second condition is true.

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

Thanks in advance.

提前致谢。

回答by Egg Vans

how about

怎么样

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

The way you have it set up now will not work as the first if will always be met, you should either do as I suggest here or put the document referer condition first (plus you need == not =).

您现在设置它的方式将不会作为第一个工作,如果将始终满足,您应该按照我在此处的建议进行操作或将文档引用条件放在首位(另外您需要 == 不是 =)。

回答by arieljuod

you should detect mobile browsers directly inside php/whateveryouuse, there's a lot of mobile detector scripts (checking the user agent how to check if the request came from mobile or computer in php)

您应该直接在 php/whateveryouuse 中检测移动浏览器,有很多移动检测器脚本(检查用户代理如何检查请求是来自移动设备还是 php 中的计算机

the way it's now: the use enters the site, if the resolution is low he is redirected to... the same page! (loaded the site twice)

现在的方式:用户进入网站,如果分辨率低,他将被重定向到...同一页面!(加载网站两次)

if you detect mobile serverside: the user enters the site only once <-- faster and works even if javascript is disabled

如果您检测到移动服务器端:用户只进入网站一次 <-- 更快,即使禁用 javascript 也能正常工作

also, you should read something about responsive designs if you only care about the browser's resolution

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

回答by lededje

Wrap the statement in an anonymous function:

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

(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;
    }
});

You can only use return from within a function, despite what the other answers claim.

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

I am also assuming that you will be adding more code beneath the if statement, otherwise there is no use in stopping execution.

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

A few issues with your code:

您的代码有几个问题:

  1. You spelt else with a capital E and is should be lowercase
  2. in the else if statement you didn't use two equals
  1. 你用大写的 E 拼写 else 并且应该是小写
  2. 在 else if 语句中你没有使用两个等于

Both issues amended in the above code

上面代码中修改的两个问题

回答by HIRA THAKUR

use window.location.hrefto get the URL

用于window.location.href获取 URL

And you have a typo:else if.. not Else if

你有一个错字: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";
}

In context of stoping the execution,

在停止执行的上下文中,

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

NOTE:There is a difference between return true,return false and return

注意:两者之间存在差异return true,return false and return

return false:stops the event from "bubbling up".Halts the execution

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

return true:continues the execution.

return true: 继续执行。

return;

返回;

Here is a quote from the ECMA 262 standard, which JavaScript is based upon, regarding return; As noted the result is "undefined".

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

An ECMAScript program is considered syntactically incorrect if it contains a return statement that is not within a FunctionBody. A return statement causes a function to cease execution and return a value to the caller. If Expression is omitted, the return value is undefined. Otherwise, the return value is the value of Expression.

如果 ECMAScript 程序包含不在 FunctionBody 中的 return 语句,则该程序被认为在语法上不正确。return 语句会导致函数停止执行并向调用者返回一个值。如果省略 Expression,则返回值未定义。否则,返回值是 Expression 的值。

回答by access_granted

IE8, IE8+, IE11, Firefox, Chrome:

IE8、IE8+、IE11、火狐、Chrome:

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

回答by ?ēēpak

If you just want to stop any javascript execution temporarily then you can use alert();function It will stop further execution until user chooses to continue it, In your case It seems you don't want to use this trick. So I guess this code may satisfy your problem.

如果您只是想暂时停止任何 javascript 执行,那么您可以使用alert();function 它将停止进一步执行,直到用户选择继续它,在您的情况下,您似乎不想使用此技巧。所以我想这段代码可以满足你的问题。

<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")
{
   alert("Hi");
   skip();
   alert("Bye");
}

Since your browser won't see any definition for skip()function, It will terminate further execution of your Javascript statements.

由于您的浏览器不会看到skip()函数的任何定义,它将终止您的 Javascript 语句的进一步执行。