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)

More in the discussion (powered by webmentions)

  • (no mentions, yet.)