admin管理员组文章数量:1401665
How could a Commando like me check if inputed code is a valid JavaScript source using some JavaScript built-in methods (if any!) or in any other possible way!? (Something like a preparser found in various IDE's like NetBeans, Eclipse, etc)?
I have to check if code is OKAY and then window.eval()-it to the current document instance.
How could a Commando like me check if inputed code is a valid JavaScript source using some JavaScript built-in methods (if any!) or in any other possible way!? (Something like a preparser found in various IDE's like NetBeans, Eclipse, etc)?
I have to check if code is OKAY and then window.eval()-it to the current document instance.
Share Improve this question edited Oct 22, 2022 at 19:34 pppery 3,81425 gold badges37 silver badges50 bronze badges asked Aug 18, 2011 at 13:41 user798596user798596 1- What do you mean by 'Okay'? whether the code should have correct syntax or should be following coding standards or should not contain malicious code? – sv_in Commented Aug 18, 2011 at 14:46
3 Answers
Reset to default 5Assuming that your need is to check whether the code will not throw any syntax errors, following is my solution:
var code = "asdfsd = 1";
try {
(function(){
new Function(code);
})()
}
catch(e) {
alert('error');
}
Working Example: http://jsfiddle/5NpGa/
You could use a lint library like:
http://www.javascriptlint./download.htm
EDIT:
Sorry, that's a piled linter and you're wanting something for on-the-fly. Try jslint: http://jslint.
You're looking for Crockford's jslint.js. It's the same code that powers http://jslint./.
本文标签: evalJavaScript syntax check with JavaScriptStack Overflow
版权声明:本文标题:eval - JavaScript syntax check with JavaScript? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744291716a2599146.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论