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.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

JavaScript | AJAX

Related posts

Comments

Comments are closed

 

About the author

Tim Vasil Tim Vasil
I'm a software engineer living in Cambridge, MA.

E-mail me Send mail

Search

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar

Recent comments