Quick JavaScript debugging tip

If you’re doing a lot of JavaScript debugging, you tend to put alert() statements all over the place (well, I do, anyway). They can interrupt things somewhat. Instead, stick this in an included JS file somewhere:

alert = function(s) { 
  var ta = document.getElementById('debug');
  if (!ta) {
    var ta = document.createElement('textarea');
    ta.id = 'debug';
    ta.rows = 8; ta.cols = 80;
    document.body.appendChild(ta);
  }
  ta.value += s+'n';
}

Then, whenever you do alert('whatever');, it’ll just add to a textarea that it creates in your page. You can scroll back through the errors, and you can put alerts inside a loop without fearing that you’ll have to click “OK” fifty times next time you test the page. (Update: doesn’t work in Internet Explorer)

I'm currently available for hire, to help you plan, architect, and build new systems, and for technical writing and articles. You can take a look at some projects I've worked on and some of my writing. If you'd like to talk about your upcoming project, do get in touch.

More in the discussion (powered by webmentions)

  • (no mentions, yet.)