我创建了一些测试代码来打开一个新窗口并尝试从父窗口捕获新窗口中的 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>
...
All of 测试.html code:
<script>
alert('test');s //error on the s
</script>
Firefox 会捕捉到这一点,但 Chrome 和 IE 不会。所有 3 个浏览器都会打开新窗口,但错误警报仅在 Firefox 中显示。每个浏览器的控制台也会显示该错误。
为什么 Chrome 和 IE 不捕获它?
还有其他方法可以让这两个浏览器捕获错误吗?
Edit:我也尝试添加w.location.href
在onerror代码之后,因为我想可能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>
我查了一下,好像返回值是window.open()
Chrome 和 IE 的行为不正常/原始window
.
看来原因是关于安全性的,以下帖子是关于 chrome 扩展的,但它可能会有所帮助:
window.open 在 Chrome 扩展中返回未定义 https://stackoverflow.com/questions/11812786/window-open-returns-undefined-in-chrome-extension
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)