我可以在Javascript中抛出一个异常来阻止Javascript执行吗? [英] Can I throw an exception in Javascript, that stops Javascript execution?

查看:480
本文介绍了我可以在Javascript中抛出一个异常来阻止Javascript执行吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试模拟一个问题,即从外部网址加载的脚本会停止在我的网站上执行更多脚本。

I try to simulate a problem where a script that is loaded from an external url stops execution of any more scripts on my site.

我试图模拟这样一个通过调用不退出的函数来解决问题。我可以在firebug中看到错误,但页面上的不同脚本仍在执行。

I tried to simulate such a problem by calling a function that does not exits. I can see the error in firebug but different scripts on the page are still executed.

Javascripts中是否存在不同类型的错误?如果是:什么样的错误会阻止脚本执行?我只需要Firefox的这个答案。

Are there different kinds of errors in Javascripts? If yes: what kind of error stops script execution? I only need this answer for Firefox.

编辑

这个问题很简单误解但Rob W得到了它:我需要抛出一个异常,该异常需要停止进一步的脚本执行。

This question is easy to misunderstood but Rob W got it: I need to throw an exception and that exception needs to stop further script execution.

推荐答案


回答标题:

回答JavaScript中是否存在不同类型的错误**:是的,请参阅MDN:错误

语法错误将阻止整个脚本块被执行,
其他错误(TypeErrors,引用错误)只会在发生后停止执行错误。

Answer to the title: No
Answer to "Are there different kinds of errors in JavaScript**: Yes, see MDN: Error
Syntax errors will prevent a whole script block from being executed, other errors (TypeErrors, Reference errors) will only stop the execution after the occurrence of the error.

不同的< script> 块是单独执行的。你不能通过在第一个块中抛出错误来防止执行第二个块(演示: http://jsfiddle.net/WJCEN/ )。

Different <script> blocks are executed separately. You cannot prevent the second block from execution by throwing an error in the first block (Demo: http://jsfiddle.net/WJCEN/).

<script>Example: Syntax error in this script.</script>
<script>console.log('Still executed.')</script>

此外,如果使用 try-catch 捕获错误(演示: http://jsfiddle.net/WJCEN/1/ ),然后错误甚至不会停止执行整个块。

Also, if an error is caught using try-catch (demo: http://jsfiddle.net/WJCEN/1/), then the error will not even stop the execution a whole block.

try {throw 'Code';}catch(e){}
console.log('Still executed');



没有通用的one-fit-all方法来阻止所有来自运行的代码。对于单个脚本,您可以使用一些技巧来阻止代码进一步运行。


There is no general one-fit-all method to stop all code from running. For individual scripts, you can use some tricks to prevent the code from running further.

1: <script>window._alert = alert;alert=null;</script>
2: <script>alert('Some code!');confirm('Not executing');</script>
3: <script>alert=_alert;delete window._alert;alert('Done!');</script>

此方法基于脚本2期望 alert 成为一个功能。我们已将 alert 重写为非函数属性(脚本1)。脚本2抛出 TypeError ,并跳过第二个块。

我们在脚本3中恢复原始值。

This method is based on the fact that script 2 expects alert to be a function. We have rewritten alert to a non-function property (script 1). Script 2 throws a TypeError, and the second block is skipped.
We restore the original values in script 3.

4. <script>Object.defineProperty(window, 'test',{value:null});</script>
5. <script>var test=function(){alert('Test');};test();alert('What?');</script>

此方法依赖于 Object.defineProperty 方法,有效地定义一个常量值。在严格模式下, var test 声明会抛出一个TypeError:test is read-only。

当没有启用严格模式时, TypeError将在 test()中抛出:test不是函数(因为我们将 test 定义为常量,在脚本4)。

This methods relies on the Object.defineProperty method, to effectively define a constant value. In strict mode, the var test declaration would throw a TypeError: "test is read-only".
When strict mode is not enables, a TypeError will be thrown at test(): "test is not a function" (because we defined test to be constant, in script 4).

注意:最后一个方法与函数声明无法正常工作(参见 bug#115452 ,Chrome 17)

Note: The last method is not working correctly with function declarations (see bug #115452, Chrome 17)

这篇关于我可以在Javascript中抛出一个异常来阻止Javascript执行吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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