如何跟踪Javascript事件(堆栈跟踪)? [英] How to trace Javascript events (Stack Trace )?

查看:109
本文介绍了如何跟踪Javascript事件(堆栈跟踪)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在任何编程语言中,我都可以跟踪任何函数并知道其他函数调用了哪个函数。但是在Javascript中,我不知道如何,因为代码不是由我编写的而且Firebug没有提供此功能 - 据我所知。

In any programming language, I can trace any function and know which function is called by other. But in Javascript , I don't know how, since the code is not written by me and Firebug does not give this feature - as far as I know.

示例:

我想显示单击XYZ元素时调用的每个函数的函数名称,以及按顺序显示它们。

I want to display the function names of each function that is called when clicking on XYZ Element, and display them in order.

谢谢。

推荐答案

发现这个: 任何浏览器中的javascript堆栈跟踪 詹姆斯说他们有现在支持github帐户

Found this: A javascript stacktrace in any browser, James says they have a github account now

function printStackTrace() {
  var callstack = [];
  var isCallstackPopulated = false;
  try {
    i.dont.exist+=0; //doesn't exist- that's the point
  } catch(e) {
    if (e.stack) { //Firefox
      var lines = e.stack.split('\n');
      for (var i=0, len=lines.length; i<len; i++) {
        if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
          callstack.push(lines[i]);
        }
      }
      //Remove call to printStackTrace()
      callstack.shift();
      isCallstackPopulated = true;
    }
    else if (window.opera && e.message) { //Opera
      var lines = e.message.split('\n');
      for (var i=0, len=lines.length; i<len; i++) {
        if (lines[i].match(/^\s*[A-Za-z0-9\-_\$]+\(/)) {
          var entry = lines[i];
          //Append next line also since it has the file info
          if (lines[i+1]) {
            entry += " at " + lines[i+1];
            i++;
          }
          callstack.push(entry);
        }
      }
      //Remove call to printStackTrace()
      callstack.shift();
      isCallstackPopulated = true;
    }
  }
  if (!isCallstackPopulated) { //IE and Safari
    var currentFunction = arguments.callee.caller;
    while (currentFunction) {
      var fn = currentFunction.toString();
      var fname = fn.substring(fn.indexOf("function") + 8, fn.indexOf('')) || 'anonymous';
      callstack.push(fname);
      currentFunction = currentFunction.caller;
    }
  }
  output(callstack);
}

function output(arr) {
  // Output however you want
  alert(arr.join('\n\n'));
}

这篇关于如何跟踪Javascript事件(堆栈跟踪)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆