【发布时间】:2013-06-28 16:22:42
【问题描述】:
我创建了一些测试代码来打开一个新窗口,并尝试从父窗口捕获新窗口中的 javascript 错误。问题是它只适用于 Firefox:
...
<body>
<script>
//open new window and keep a reference to it
var w = window.open('test.html', '_blank'); //test.html has an error
//add the error listener
w.onerror = function(msg, file, line) { alert('error'); };
</script>
</body>
...
所有test.html代码:
<script>
alert('test');s //error on the s
</script>
Forefox 会捕捉到这一点,但 Chrome 和 IE 不会。所有 3 个浏览器都将打开新窗口,但错误警报仅在 Firefox 中显示。每个浏览器的控制台也会显示错误。
为什么 Chrome 和 IE 不能捕获它?
还有其他方法可以让这两个浏览器捕获错误吗?
编辑:
我还尝试在 onerror 代码后添加w.location.href,因为我认为可能是 onerror 代码执行得太晚了。这并没有影响我的结果。 Firefox 仍然是唯一一个发现错误的。
<script>
var w = window.open('test2.html', '_blank'); // test 2 is a page with no errors
w.onerror = function(msg, file, line) { alert('error'); };
w.location.href = 'test.html'; // the page with the error
</script>
【问题讨论】:
标签: javascript