Trying to work with some Javascript that keeps popping up an alert box. The library is huge and not particularly well organised, so I wrapped the original window.alert in one that pops up another alert box with a stack trace just before the one the library pops up. You could use this to find out where any function is called be changing the function it wraps in the last four lines.

I'm sure this isn't particularly pretty Javascript - if you know a more idiomatic way to write it, please let me know in the comments.

var original_alert = window.alert;

var stacktrace = function() {
  var regex = /function\W+([\w-]+)/i;

  var callee = arguments.callee;
  var trace = "";
  while(callee) {
    trace += (regex.exec(callee))[1] + '(';

    for (i = 0; i < callee.arguments.length - 1; i++) {
      trace += "'" + callee.arguments[i] + "', ";

    if (arguments.length > 0) {
      trace += "'" + callee.arguments[i] + "'";

    trace += ")\n\n";

    callee = callee.arguments.callee.caller;

window.alert = function(msg) {
written by
Disagree? Found a typo? Got a question?
If you'd like to have a conversation about this post, email I don't bite.
You can verify that I've written this post by following the verification instructions:
curl -LO
curl -LO
gpg --verify debugging-javascript-with-a-stacktrace.html.orig{.asc,}