admin管理员组文章数量:1384639
I have following code to capture errors in javascript
<script type="text/javascript">
window.onerror = function(msg, url, linenumber) {
console.log('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);
return true;
}
</script>
I opened chrome developer console and going to generate ReferenceError
by random javascript code. Lets say:
person.run(); // person object does not exist
It throws Uncaught ReferenceError: person is not defined
and printed in the console. But it is not captured by window.onerror
. Why?
I have following code to capture errors in javascript
<script type="text/javascript">
window.onerror = function(msg, url, linenumber) {
console.log('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber);
return true;
}
</script>
I opened chrome developer console and going to generate ReferenceError
by random javascript code. Lets say:
person.run(); // person object does not exist
It throws Uncaught ReferenceError: person is not defined
and printed in the console. But it is not captured by window.onerror
. Why?
- the code should run without issues, are you sure you're not running person.run before you load the onerror, or is there something else that could interfere – Master Slave Commented Oct 26, 2014 at 6:21
-
I am just typing
person.run()
in chrome developer console. I want to know errors happen in chrome developer console(by your own try out codes) captured bywindow.onerror
? – Fizer Khan Commented Oct 26, 2014 at 6:24 - try window.alert(window.foo) – mplungjan Commented Oct 26, 2014 at 6:28
- @mplungjan What do you mean? – Fizer Khan Commented Oct 26, 2014 at 6:30
- Try something that might invoke a window error – mplungjan Commented Oct 26, 2014 at 8:21
2 Answers
Reset to default 3An event handler for runtime script errors.
Note that some/many error events do not trigger window.onerror, you have to listen for them specifically.
open this link this is use full for you:
https://developer.mozilla/en-US/docs/Web/API/GlobalEventHandlers.onerror
For console or for other browser app
document.onerror=function (errorMsg, url, lineNumber,colNumder,error){
console.log('OnError');
return true
}
function consoleErrorTrap(call){
try{
return call();
} catch(e){
let event= new ErrorEvent('error',{error:e})
document.dispatchEvent(event);
}
}
consoleErrorTrap(()=>{
throw new Error()
})
本文标签: jquerywindowonerror and javascript console errorsStack Overflow
版权声明:本文标题:jquery - window.onerror and javascript console errors - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744537661a2611413.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论