onerror事件捕获网页中的错误
转载请注明来源:https://www.cnblogs.com/hookjc/
<html>
<head>
<script type="text/javascript">
onerror=handleErr
var txt=""
function handleErr(msg,url,l)
{
txt="There was an error on this page.\n\n"
txt+="Error: " + msg + "\n"
txt+="URL: " + url + "\n"
txt+="Line: " + l + "\n\n"
txt+="Click OK to continue.\n\n"
alert(txt)
return true
}
function message()
{
adddlert("Welcome guest!")
}
</script>
</head>
<body>
<input type="button" value="View message" onclick="message()" />
</body>
</html>
注:当点击按钮时出现错误,则触发onerror事件 即onerror=handleErr 去执行handleErr方法,msg为错误信息,url为当前显示的url地址,l表示错误的行号