最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - catch SyntaxError and run alternate function - Stack Overflow

programmeradmin3浏览0评论

I'm trying to build something on javascript that I can have an input that can be everything like string, xml, javascript and (non-javascript string without quotes) as follows:

//strings
    eval("'hello I am a string'"); /* note the following proper quote marks */

//xml
    eval(<p>Hello I am a XML doc</p>);

//javascript
    eval("var hello = 2+2;");

So this first 3 are working well since they are simple javascript native formats

but when I try use this inside javascript

//plain-text without quotes
    eval("hello I am a plain text without quotes");
    //--SyntaxError: missing ; before statement:--//

Obviously javascript interprets this as syntax error because it thinks its javascript throwing a SyntaxError.

So what I would like to do it to catch this error and perform the adjustment method if this occurs.

I've already tried with try catch but it doesn't work since it keeps returning the Syntax error as soon as it tries to execute the code.

Any help would be much appreciated

Cheers :)

Additional Information: Imagine an external file that javascript would read, using spidermonkey, so it's a non-browser stuff(I can't use HttpRequest, DOM, etc...)..not sure if this matters, but there it is. :)

I'm trying to build something on javascript that I can have an input that can be everything like string, xml, javascript and (non-javascript string without quotes) as follows:

//strings
    eval("'hello I am a string'"); /* note the following proper quote marks */

//xml
    eval(<p>Hello I am a XML doc</p>);

//javascript
    eval("var hello = 2+2;");

So this first 3 are working well since they are simple javascript native formats

but when I try use this inside javascript

//plain-text without quotes
    eval("hello I am a plain text without quotes");
    //--SyntaxError: missing ; before statement:--//

Obviously javascript interprets this as syntax error because it thinks its javascript throwing a SyntaxError.

So what I would like to do it to catch this error and perform the adjustment method if this occurs.

I've already tried with try catch but it doesn't work since it keeps returning the Syntax error as soon as it tries to execute the code.

Any help would be much appreciated

Cheers :)

Additional Information: Imagine an external file that javascript would read, using spidermonkey, so it's a non-browser stuff(I can't use HttpRequest, DOM, etc...)..not sure if this matters, but there it is. :)

Share Improve this question edited Mar 12, 2010 at 22:52 zanona asked Mar 12, 2010 at 21:51 zanonazanona 12.7k25 gold badges90 silver badges146 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 21

Are you sure a Try...Catch block won't work? This example works for me in firefox.

try {
  eval("hello I am a plain text without quotes");
} catch(err) {
  alert("error caught");
}
发布评论

评论列表(0)

  1. 暂无评论