开发者

Is it possible to programatically catch JavaScript SyntaxErrors?

开发者 https://www.devze.com 2022-12-24 15:00 出处:网络
I don\'t think that this is doable, but wanted to throw it out to the community to confirm my suspicions.

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=开发者_如何学Python"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);
    }
  }


Google has released their Closure toolset which includes a JavaScript compiler and minimizer. I haven't used it personally but have heard a lot of great things. Also, it apparently helps you locate browser compatibility issues.

I don't think it will help you to perform real-time analysis, but still could be a valuable tool.


If you are comfortable with the security exposure, you could call eval() on the code in question to detect syntax errors.

function say(x) { ... emit message in some way.... }

var scriptlets = [
    "foof1 = function(a) {if (a == 7) { return \"A-OK\"; } } ",
    "foof2 = function (a) {if argle bargle seventy 7, then this isn't going to work}"
];

function verifyScriptlet(n) {
    var s = scriptlets[n];
    try {
        var x = eval(s);
        say("verified #"+ n +", result: " + (typeof x));
    }
    catch (exc1)
    {
        say("Exception while verifying #"+ n +": " + exc1.message);
    }
}

verifyScriptlet(0);
verifyScriptlet(1);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号