[英]Getting Javascript Errors From Another Window
I created some test code to open a new window and attempt to catch javascript errors in the new window, from the parent window. 我创建了一些测试代码以打开一个新窗口,并尝试从父窗口捕获新窗口中的javascript错误。 The problem is that it only works in Firefox:
问题在于它仅在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 test.html code: 所有test.html代码:
<script>
alert('test');s //error on the s
</script>
Forefox will catch this, but Chrome and IE do not. Forefox将抓住这一点,但Chrome和IE不会。 All 3 browsers will open the new window, but the error alert only displays in Firefox.
所有3个浏览器都将打开新窗口,但错误警报仅在Firefox中显示。 Each of the browsers' consoles also show the error.
每个浏览器的控制台也会显示该错误。
Why don't Chrome and IE catch it? Chrome和IE为什么不赶上它?
Is there some other way to make those 2 browsers catch the errors? 还有其他方法可以使这两个浏览器捕获错误吗?
Edit: I also tried add w.location.href
after the onerror code, because I thought that maybe the onerror code was executed too late. 编辑:我也尝试在onerror代码之后添加
w.location.href
,因为我认为也许onerror代码执行得太晚了。 This did not affect my results. 这并没有影响我的结果。 Firefox was still the only one to catch the error.
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>
I did a little searching, it seems the return value of window.open()
of Chrome and IE are not act as normal/original window
. 我做了一些搜索,Chrome和IE的
window.open()
的返回值似乎不充当普通/原始window
。
It seems the reason is about security, the following post is about chrome extension, but it may help: 看来原因与安全性有关,以下博文与chrome扩展有关,但可能会有所帮助:
window.open returns undefined in chrome extension window.open在chrome扩展名中返回未定义
Your best bet is going to be to wrap any code in the new window in a try/catch block. 最好的选择是将新窗口中的所有代码包装在try / catch块中。 Catch the errors then send them back as such:
捕获错误,然后将其发送回:
catch ... {
window.opener.console.log(message);
}
Try with adding some timeout 尝试添加一些超时
setTimeout(function(){w.onerror = function(msg, file, line) { alert('error'); };}, 3000) setTimeout(function(){w.onerror = function(msg,file,line){alert('error');};},3000)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.