简体   繁体   English

如何从JavaScript错误行号获取实际行

[英]How to get actual line from javascript error line number

The line # isn't so useful when your page is dynamically generated. 动态生成页面时,#号不是很有用。 What I'd really like is the actual offending line. 我真正想要的是实际的违规行为。

Is there some way of doing this? 有什么办法吗? Perhaps 也许

document.getElementsByTagName('html')[0].innerHTML.Split('\n')[lineNumber-1];

// OR

document.body.innerHTML.Split('\n')[lineNumber-1];

However, when I tested it in Firefox 10 , I got that the above were inaccurate unless the html and body tags were on the same line. 但是,当我在Firefox 10对其进行测试时,除非htmlbody标签在同一行上,否则我认为上述内容是不正确的。

Thanks in advance. 提前致谢。

For anyone who is curious, the answer for me was to make the number of lines above the first head or body element consistent across all pages. 对于任何好奇的人,我的答案是使所有页面的第一个headbody元素上方的行数保持一致。

For me this magic # is three. 对我来说,这神奇的是三。

One should note though that if the error occurred in a js file, then I don't know how to get the actual line client-side. 应该注意的是,如果该错误发生在js文件中,则我不知道如何获取客户端的实际行。 However, since the whole point of this was to log the line to the server (using ajax), I could simply read the line of the js file on the server. 但是,由于这样做的全部目的是将行记录到服务器(使用ajax),因此我可以简单地读取服务器上js文件的行。 So it means that you have to ad an extra check on the url of the error to see that it is or is not a javascript file. 因此,这意味着您必须对错误的网址进行额外检查,以查看它是否是一个javascript文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM