Debugging dynamically evaulated JavaScript

by timvasil 4/22/2008 2:10:00 AM

When using AJAX, an easy way to evaluate JavaScript or JSON returned from a web server is the good ol' eval function.  However, if an error is encountered during the evaluation Microsoft's Script Debugger doesn't let you see the offending line.  (I haven't checked what FireBug does yet.)

To get around this problem, you can insert the JavaScript as a <script> DOM node. 

In other words, instead of this:

    eval(js);

try this:

    var scriptTag = document.createElement("script");
    scriptTag.setAttribute('type', 'text/javascript');
    scriptTag.text = js;
    document.body.appendChild(scriptTag);

With the latter approach, the debugger works great.

Tags:

JavaScript | AJAX

Comments are closed

Search

Calendar

«  February 2012  »
SuMoTuWeThFrSa
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910

View posts in large calendar

Recent comments

Archive