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

html - Running raw javascript (from plain text) - Stack Overflow

programmeradmin0浏览0评论

I'm trying to execute JavaScript from plain text (from being entered by a client). I also need a way to see if the executed code works or not (if it does, then it does, otherwise, it needs to spit out a non-variable error message).

Thanks if you can! The stuff that will be executed would be short strings such as:

echo("a","b")

I'm trying to execute JavaScript from plain text (from being entered by a client). I also need a way to see if the executed code works or not (if it does, then it does, otherwise, it needs to spit out a non-variable error message).

Thanks if you can! The stuff that will be executed would be short strings such as:

echo("a","b")
Share Improve this question edited May 3, 2014 at 0:16 Chris 1,42618 silver badges29 bronze badges asked May 13, 2011 at 17:04 FreesnöwFreesnöw 32.2k31 gold badges94 silver badges140 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

You can use eval and wrap around try-catch.

try
  {
    eval(code);
  }
catch(err)
  {
  //Handle errors here
  }

Are you saying you just need a try/catch statement?

https://developer.mozilla/en/JavaScript/Reference/Statements/try...catch

发布评论

评论列表(0)

  1. 暂无评论