在打印arguments.callee.caller时,除了IE浏览器之外,它是null

时间:2015-09-03 10:58:36

标签: javascript jquery asp.net asp.net-mvc

我使用window.onerror方法来利用全局异常。我试图通过在IE中使用arguments.callee.caller来获取函数名称。但是在chrome和所有其他浏览器中它都是null。我google了很多。但我没有得到任何结果。如何在chrome / mozilla中获取调用者的值。请建议我。我给出了下面给出的示例代码。

window.onerror = function (msg, url, line, col, error) {
        debugger;
        // Note that col & error are new to the HTML 5 spec and may not be 
        // supported in every browser.  It worked for me in Chrome.
        var extra = !col ? '' : '\ncolumn: ' + col;
        extra += !error ? '' : '\nerror: ' + error;

        LineNumber = line;
        ColumnNumber = col;
        ErrorDescription = error.stack;
        //console.log(ErrorDescription);
        ErrorString = msg;
        var now = new Date();
        var datetime = now.getFullYear() + '/' + (now.getMonth() + 1) + '/' + now.getDate();
        ErrorDateTime = datetime + ' ' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();

        // You can view the information in an alert to see things working like this:
        //alert("Error: " + msg + "\nurl: " + url + "\nline: " + line + extra);

        var FilePath = window.location.pathname;
        PageName = FilePath.split("/").pop();

        var ExceptionFunctionName = window.onerror.caller;
        if (ExceptionFunctionName != undefined && ExceptionFunctionName != null) {
            ExceptionFunctionName = ExceptionFunctionName.substr('function '.length);
            MethodName = ExceptionFunctionName.substr(0, ExceptionFunctionName.indexOf('{'));
        }
        capture();
        var suppressErrorAlert = true;
        // If you return true, then error alerts (like in older versions of 
        // Internet Explorer) will be suppressed.
        //return suppressErrorAlert;
    }

我的呼叫功能如下。

 function ExceptionFunction() {
      var Test = testvar / 5;
  }

1 个答案:

答案 0 :(得分:0)

而不是window.onerror.caller您需要使用arguments.callee.caller

BR, 萨尔

相关问题