I have a tool similar to JSFiddle which allows me to dynamically inject javascript and run it on the page. The code can be several lines and usually will be.
Unfortunately, if there is an exception in the code I entered, I cannot get the exception line number if I use eval () to run the code.
I found a partial solution that instead of using
try{ eval(code); } catch(e) { processException(e); }
to do something like this:
var s = document.createElement('script'); s.appendChild(document.createTextNode( "try{\n" + code + "}catch(e){processException(e)}")); document.body.appendChild(s);
Now, if the code throws an exception and I look at the stack trace (in my function processException ()), I can get the line number of the exception (in firefox and chrome, anyway).
This is good and good if it is actually a run-time exception, such as a variable that is not defined. The problem is a syntax parsing error, such as inconsistent parsers, etc. I get nothing.
Is there a crazy workaround for this that works on firefox and chrome at least? Eval inside eval in script tag inside Function object? I try my best and found nothing.
rob
source share