admin管理员组文章数量:1178553
I have a textarea where user can enter javascript code which upon press of the button would be passed to eval().
I am having trouble catching the referenceError for cases when a user enters something like this:
var myName = Maria;
instead of
var myName = "Maria";
Thank you for you time!
I have a textarea where user can enter javascript code which upon press of the button would be passed to eval().
I am having trouble catching the referenceError for cases when a user enters something like this:
var myName = Maria;
instead of
var myName = "Maria";
Thank you for you time!
Share Improve this question asked Sep 12, 2015 at 9:27 jacobdojacobdo 1,6154 gold badges16 silver badges34 bronze badges 7 | Show 2 more comments2 Answers
Reset to default 30Ok, as you said you understood the pit's of eval()
, here i'm proposing a solution.
try {
var myName = Maria;
} catch (e) {
if (e instanceof ReferenceError) {
// Handle error as necessary
}
}
Try putting a try/catch block around the eval() call. Like this:
try {
eval(userInput);
} catch (e) {
// do something
}
(Note that passing user input to eval() is NOT something you should do on a real site, for security reasons.)
本文标签: javascriptCatch a referenceError in jsStack Overflow
版权声明:本文标题:javascript - Catch a referenceError in js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738107941a2064429.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
eval()
and executing user code in the browser, please make sure you know what you're doing before using anything like this in production – hammus Commented Sep 12, 2015 at 9:31