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 @ 2022-10-07 20:58  yinghualeihenmei  阅读(853)  评论(0编辑  收藏  举报