eval javascript, check for syntax error

后端 未结 6 505
失恋的感觉
失恋的感觉 2020-12-08 09:50

I wanted to know if it is possible to find through javascript if a call to eval() has a syntax error or undefined variable, etc... so lets say I use eval for some arbitrary

6条回答
  •  情书的邮戳
    2020-12-08 10:27

    According to the Mozilla documentation for eval:

    eval returns the value of the last expression evaluated.

    So I think you may be out of luck. This same document also recommends against using eval:

    eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. More importantly, third party code can see the scope in which eval() was invoked, which can lead to possible attacks in ways of which the similar Function is not susceptible.

    So regardless, please be aware of the risks before using this function.

提交回复
热议问题