我正在使用try,catch进行调试,但警告不是创建异常。如何获取所有javascript警告和错误输出div?
更新: 如果浏览器支持Afaik日志记录,如何将该日志转换为字符串或输出div?
更新: 我找到了如何做到这一点的方式: 我可以将console.log函数重新加载到我的自定义函数和调用本机console.log函数。
答案 0 :(得分:0)
首先,摆脱try catch。在调试时不要使用try catch。
其次,你不想把错误输出到div,使用firebug或者检查器 - console.log();
第三,如果你真的想这样做:你可以使用try catch并在catch中使用类似
的东西$('body').append($('div').html('variable for error message goes here'));
如果您使用的是jquery
OR
document.getElementByTagName("body").appendChild( document.createTextNode("variable for error message goes here") );
如果你有简单的javascript
编辑:尝试查找ie debug bar,ie webDeveloper
答案 1 :(得分:0)
对于IE javascript调试,您可以按照本指南进行操作:
http://msdn.microsoft.com/en-au/library/ie/gg699336(v=vs.85).aspx
请记住,在加载页面之前必须打开开发人员工具窗口,以便在控制台中显示警告和错误。
对于webkit,(chrome,safari)开发者控制台 - 这是一个指南:
https://developers.google.com/chrome-developer-tools/docs/console
... Firefox还有一个控制台
答案 2 :(得分:0)
我理解自己为什么当文档中出现错误时,某人可能想要某些内容实际发生。上面的答案只是说你会使用开发者工具,但我需要实际发生的事情,经过一些搜索,这就是我发现的......
如果您希望捕获所有错误,可以将以下代码放入您的文件中,最好在顶部:
window.onerror = function(errorMsg, url, lineNumber){
// any action you want goes here
// errorMsg is the error message itself.
// url should be the file presenting the error, though i have
// found that it only presents to me the address of the site.
// lineNumber is the line number the error occoured on.
// here is an example of what you could do with it:
alert("Error in " + url + " at " + lineNumber + ":\n" + errorMsg);
}
我,我自己,喜欢将错误输出到包含所有错误的div,尽管你可以对这些信息做任何事情,你可以对传递给函数的任何其他字符串做。
以下是使用上述代码使用按钮抛出错误时可能发生的情况的示例:
你的.site.here错误1:
未捕获的ReferenceError:未定义foo