Catch Error link in js

I have a text box where the user can enter javascript code, which after clicking the button will be passed to eval ().

I am having trouble intercepting a referenceError for cases where the user enters something like this:

var myName = Maria;

instead

var myName = "Maria";

Thank you for your time!

+4
source share
2 answers

Well, as you said, you understood the pit eval(), here I offer a solution.

  try {
      var myName = Maria;
  } catch (e) {
      if (e instanceof ReferenceError)) {
      // take necessary steps
  }
+7
source

Try putting a try / catch block around the call to eval (). Like this:

try {
    eval(userInput);
} catch (e) {
    // do something
}

( , eval() -, .)

+2

Source: https://habr.com/ru/post/1607063/


All Articles