Alex G Alex G - 1 year ago 229
jQuery Question

Check syntax of JavaScript(jQuery) code before running jQuery.globalEval() or eval()

Continuing with topic: jQuery .globalEval() function

I use jQuery.globalEval(varwithJScode);

Now the problem starts when code in varwithJScode contains syntax or other error - the whole script stops.

jsFiddle does not support jQuery.globalEval(), but I used eval() instead to simulate.
Try changing var codeforeval to some wrong javascript code and next command will fail.

Is there a way to avoid other JS code from stopping in case of error and/or is there a way to check syntax before eval?

Tried using this one:

try {
} catch (e) {
if (e instanceof SyntaxError) {
alert('JS error!');

Does not work for me!

Answer Source
            try {
            } catch(e) {