[英]How to trigger script.onerror in Internet Explorer?
The onerror page on MSDN states that the onerror handler can be attached to a script element and that it "Fires when an error occurs during object loading.". MSDN上的onerror页面指出,onerror处理程序可以附加到脚本元素,并且“在对象加载期间发生错误时触发”。
For the purpose of unit tests, I am trying to get this onerror handler to fire, but could not find a suitable example. 出于单元测试的目的,我试图让这个onerror处理程序触发,但找不到合适的例子。
The following code triggers an error in Firefox, but no alert is displayed in Internet Explorer 以下代码在Firefox中触发错误,但Internet Explorer中不显示任何警报
<script src="http://www.google.com/NOTFOUND.js" onerror="alert('error fired')"></script>
Does anyone know a value for script.src that would fire the handler attached to script.onerror? 有没有人知道script.src的值会触发附加到script.onerror的处理程序?
I found this buried in some MSDN documentation : 我发现这隐藏在一些MSDN文档中 :
Note that the documentation mistakenly says this works for elements too; 请注意,文档错误地说这也适用于元素; the error will be fixed in the Workshop documentation for the final release of Internet Explorer 5 in March. 错误将在3月份Internet Explorer 5最终版本的Workshop文档中修复。
The next thing I thought of that could help is the onreadystatechange event: 接下来我想到的就是onreadystatechange事件:
<script src="http://www.google.com/NOTFOUND.js" onreadystatechange="alert(this.readyState)">
This event fires twice for me, once with "loading" and again with "loaded", whether the script is valid or not. 这个事件对我来说是两次,一次是“加载”,另一次是“加载”,无论脚本是否有效。 Other documentation I've found says that sometimes it fires a complete event, and it's not really clear when it's supposed to fire. 我发现的其他文档说,有时它会触发一个完整的事件,并且它应该在什么时候发射它并不是很清楚。 So it looks like that won't work. 所以它看起来不会起作用。
So I think you're left with the hacky solution of checking that a variable which the script is supposed to declare really exists. 所以我认为你留下了一个hacky解决方案,检查脚本应该声明的变量是否确实存在。 In HTML: 在HTML中:
<script src="http://yourdomain.com/declare_foo.js"></script>
<script>if (typeof foo == "undefined") {alert ('error loading script');}</script>
And then of course in declare_foo.js, you'd have 然后当然在declare_foo.js中,你有
var foo = 'Script loaded successfully';
Having just done a bit of reading on this, it looks like onerror can also be attached to the window object. 刚刚对此进行了一些阅读,看起来onerror也可以附加到window对象上。 See: 看到:
http://www.javascriptkit.com/javatutors/error.shtml http://www.javascriptkit.com/javatutors/error.shtml
According to that page, you can pass in msg, url and linenumber arguments: 根据该页面,您可以传入msg,url和linenumber参数:
window.onerror=function(msg, url, linenumber){
alert('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber)
return true
}
Not sure on the browser support for this, but thanks for bringing it to my attention! 不确定浏览器是否支持此功能,但感谢您引起我的注意!
Update: didn't do anything in Safari 4 (apart from logging an error in firebug as per usual), but shows the alert in Firefox 3.6 Beta 5. Error test at http://www.javascriptkit.com/javatutors/errortest2.htm 更新:在Safari 4中没有做任何事情(除了按照常规记录firebug中的错误),但在Firefox 3.6 Beta 5中显示警报。在http://www.javascriptkit.com/javatutors/errortest2上进行错误测试。 HTM
Update 2: done a test with the following: 更新2:使用以下内容完成测试:
index.html: index.html的:
<html>
<head>
<script type="text/javascript">
window.onerror=function(msg, url, linenumber){
alert('Error message: '+msg+'\nURL: '+url+'\nLine Number: '+linenumber)
return true
}
</script>
<script type="text/javascript" src="js.js"></script>
</head>
<body>
</body>
</html>
js.js: (stupid name, i know!) js.js :(愚蠢的名字,我知道!)
document.write('hi there'
Result in FF 3.6 Beta 5: FF 3.6 Beta 5中的结果:
How about this? 这个怎么样?
<script language="JavaScript" type="text/javascript">
var DidItLoad=0;
function funcDidItLoad()
{
if(DidItLoad==1)
{
alert("good")
}
else
{
alert("bad")
}
DidItLoad=0;
return 0;
}
</script>
<script language="JavaScript" type="text/javascript" onload="DidItLoad=1;" src="javascript/myError.js"></script><!--erase me-->
<script language="JavaScript" type="text/javascript">
funcDidItLoad();
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.