addEventListener和之间有什么区别onclick?
var h = document.getElementById("a");
h.onclick = dothing1;
h.addEventListener("click", dothing2);
Run Code Online (Sandbox Code Playgroud)
上面的代码一起存在于一个单独的.js文件中,它们都完美地工作.
我有一些JavaScript(Jquery Tools'Overlay),当它被丢弃在错误地使用它的页面上时可能抛出异常,我正在尝试优雅地处理它.
我有一个通用的window.onerror处理程序来拯救这些错误并将它们报告回服务器,但是没有被触发.
我也无法在这段代码中包装一个try/catch,因为它被包含在HTML中的远程脚本中.
关于如何挽救外部脚本引发的错误的任何想法?
更新:这是一个例子.我应该纠正自己,window.onerror 会被触发,但是脚本不会继续运行(在示例中,警报从不警告).
<html>
<script type="text/javascript">
window.onerror = function(e){ console.log("caught error: "+e); return true;}
</script>
<body>
<!-- this is the line in the dom that causes the script to throw -->
<a rel="nofollow"></a>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">google.load("jquery", "1.4.1");</script>
<script src="http://cdn.jquerytools.org/1.2.5/tiny/jquery.tools.min.js"></script>
<script type="text/javascript">
//this code will throw an error in jquery tools
$("a[rel]").overlay();
alert("it's ok, I still ran.");
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)