是否可以编程捕捉JavaScript语法错误? [英] Is it possible to programatically catch JavaScript SyntaxErrors?

查看:82
本文介绍了是否可以编程捕捉JavaScript语法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不认为这是可行的,但是想把它交给社区来确认我的怀疑。假设您正在使用Python,PHP或ASP等其他语言编写程序。该程序旨在构建另一个使用JavaScript编写的程序。然而,第一个程序不幸的是不能免于错误。因此,偶尔,构建JavaScript程序的程序会产生一些简单的东西,并在JavaScript源中输出语法错误。现在有些用户去加载程序,它基本上停止,因为运行它的Web浏览器无法正确解析JavaScript。这个用户可能不会很开心。我不会这样。



这带给我我的问题。是否可以编写一个错误处理程序来捕获这些语法问题,从而允许应用程序正常失败?



这是一个例子:

 < html> 
< head>
< script type =text / javascriptcharset =utf-8>
window.onerror = errorHandler;
function errorHandler(a,b,c){
alert('horray!No wait,Booo!');
}
vara jfs;
< / script>
< / head>
< body>
可以这样做吗?
< / body>
< / html>

 < HTML> 
< head>
< script type =text / javascriptcharset =utf-8>
try {
vara jfs;
} catch(e){
alert('horray!No wait,Booo!');
}
< / script>
< / head>
< body>
可以这样做吗?
< / body>
< / html>


解决方案

通常,一种编程语言被解析,如果成功可能编译成另一种形式,如果成功,则在硬件或vm上进行解释/执行。只是因为代码通过了一个阶段,并不意味着它是有效的代码。



您正在询问在上一阶段中运行的代码是否能够识别第一部分遇到的问题并处理此问题。



某些语言为自己的解析和编译机制提供了各种钩子。正如Cheeso所说, eval 是Javascript调用此机制的方法。



而不是将所有脚本都包含在字符串,将它们放在javascript文件中可能更好,并使用以下javascript(或类似的)将它们加载到主程序中。

  function load(file){
var client = new XMLHttpRequest();
client.open(GET,file,false);
client.send(null);
return client.responseText;
}

函数include(file){
try {
eval(load(file));
} catch(e){
alert(file with file+ file);
}
}


I don't think that this is doable, but wanted to throw it out to the community to confirm my suspicions.

Let's say you're writing a program in another language like Python, PHP, or ASP. This program is intended to build another program written in JavaScript. However, the first program is unfortunately not immune to errors. So, occasionally, the program which builds the JavaScript program does some funky stuff and outputs a syntax error in the JavaScript source. Now some user goes and loads the program and it essentially halts, because the web browser running it can't properly parse the JavaScript. This user is probably not going to be happy. I wouldn't be.

This brings me to my question. Is it possible to write an error handler that would catch these kind of syntax problems allowing the application to fail gracefully?

Here's an example:

    <html>
  <head>
   <script type="text/javascript" charset="utf-8">
    window.onerror = errorHandler;
    function errorHandler(a,b,c) {
     alert('horray!  No wait, Booo!');
    }
    vara jfs;
   </script>
  </head>
  <body>
   Can this be done?
  </body>
 </html>

or

<html>
  <head>
   <script type="text/javascript" charset="utf-8">
    try {
     vara jfs;
    } catch  (e) {
     alert('horray!  No wait, Booo!');
    }
   </script>
  </head>
  <body>
   Can this be done?
  </body>
 </html>

解决方案

Generally, a programming language is parsed, and if that succeeds possibly compiled to another form, and if that succeeds then interpreted/executed on hardware or a vm. Just because code passes one phase, doesn't mean it's valid code.

You are asking if code running in the last phase, can be aware of problems encountered in the first part and deal with it.

Some languages offer various hooks into it's own parsing and compiling mechanism. As Cheeso mentioned, eval is Javascript's way of calling into this mechanism.

Rather than wrapping all of your scripts in strings, it might be nicer to leave them in javascript files, and load them into your main program with the following javascript (or similar).

 function load(file){
    var client = new XMLHttpRequest();
    client.open("GET", file, false);
    client.send(null);
    return client.responseText;
  }

  function include(file){
    try {
      eval(load(file));
    } catch (e) {
       alert("problem with file " + file);
    }
  }

这篇关于是否可以编程捕捉JavaScript语法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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