Javascript 例外情况。行号返回“;“未定义”;在google chrome和internet explorer中
我需要从javascript异常获取文件名、消息、行号等。我尝试了以下代码Javascript 例外情况。行号返回“;“未定义”;在google chrome和internet explorer中,javascript,exception-handling,cross-browser,line-numbers,Javascript,Exception Handling,Cross Browser,Line Numbers,我需要从javascript异常获取文件名、消息、行号等。我尝试了以下代码 try { alertt("dddd"); } catch (e) { console.log("ExceptionType: "+ e.name); console.log("Message: "+ e.message); console.log("Line No: "+ e.lineNumber); } 我在Mozilla Firefox中得到了以下结果 例外类型:引用错误 消息:未定义警报 第4行
try {
alertt("dddd");
} catch (e) {
console.log("ExceptionType: "+ e.name);
console.log("Message: "+ e.message);
console.log("Line No: "+ e.lineNumber);
}
我在Mozilla Firefox中得到了以下结果
例外类型:引用错误消息:未定义警报
第4行 但同样的代码在Google Chrome、Internet Explorer上给出了以下结果 例外类型:引用错误
消息:未定义警报
行号:未定义 它没有给出行号。如何解决这个问题?有没有其他方法来获取行号 我尝试了e.stack它以字符串形式返回堆栈跟踪。 它在Google Chrome中给了我以下输出
ReferenceError: alertt is not defined
at message (http://localhost/ems-test/js/test.js:4:4)
at HTMLDocument.<anonymous> (http://localhost/ems-test/js/test.js:14:2)
at c (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:26036)
at Object.p.fireWith [as resolveWith] (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:26840)
at Function.x.extend.ready (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:3305)
at HTMLDocument.q (http://localhost/ems-test/js/jquery-1.10.2.min.js:4:717)
两者都是字符串类型的结果。不是一个物体。所以它需要从这个巨大的字符串中提取行号。但问题是两者的结果并不相同。一个在第一行显示行号,另一个在第二行显示行号。所以很难从这个巨大的字符串中提取行号
是否有任何方法将堆栈跟踪作为对象获取
window.onerror = function (msg, url, line) {
alert("Message : " + msg );
alert("url : " + url );
alert("Line number : " + line );
}
希望这能对你有所帮助。
查看此链接:我的机器上没有IE,因此我不能就此发言;但是在Chrome中,您可以通过查看e.stack属性并解析它来获得所需的内容。如果在catch块中执行console.dir(e),您可以看到可用的选项。错误对象API未标准化。是否有其他方法获取行号?我不知道在Internet Explorer或Chrome/Safari中有什么方法,但也许有人会,也许stack包含行号:e.stack查看这个stacktrace库,这是我已经做过的。我的要求是在try…catch.中获取此内容。在使用window.onerror()时,我们将获取行号。但是如何在使用try{}catch{}时获得它呢?
window.onerror = function (msg, url, line) {
alert("Message : " + msg );
alert("url : " + url );
alert("Line number : " + line );
}