window.onerror事件用法

https://blog.csdn.net/legoe/article/details/7007152

https://www.cnblogs.com/otakuhan/p/7802881.html

在页面没有错误时,window.onerror事件是不存在的;

如果错误被try..catch语句捕捉到的话,那么onerror事件就不会产生,当场面上有没有被处理的exception的时候onerror事件才会发生;

window.onerror = function (message, url, line)
{
alert("Message : " + message + "\nURL : " + url + "\nLine Number : " + line);
// Return true to supress the browser error messages (like in older versions of Internet Explorer)
return true;
}

try
{
NonExistingFunction();
}
catch (e)
{
document.write(e.message);
}

Output : 'NonExistingFunction' is undefined;

当一个错误如下例中一样出现后,我们想要将一个函数赋给window.onerror事件.这个函数作为onerror事件的时间处理者,有3个参数:

message表示错误信息

URL表示错误出现的位置

line表示错误出现在第几行

window.onerror = function (message, url, line)
{
alert("Message : " + message + "\nURL : " + url + "\nLine Number : " + line);
// Return true to supress the browser error messages (like in older versions of Internet Explorer)
return true;
}

NonExistingFunction();

posted @   yinghualeihenmei  阅读(917)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· Obsidian + DeepSeek:免费 AI 助力你的知识管理,让你的笔记飞起来!
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示