简体   繁体   English

从另一个窗口获取Javascript错误

[英]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.

相关问题 从另一个JavaScript文件调用一个JavaScript函数,并在atom IDE中获取参考错误和无效状态错误以及一些警告 - Calling a javascript function from another javascript file and getting Reference errors and Invalid state errors and some warnings inside atom IDE javascript从另一个窗口关闭一个窗口会覆盖调用窗口的名称 - javascript closing a window from another window overwrites calling window name 在javascript中使用window.open()时,从php页面获取所有数据并将其发送到另一个页面 - Getting all data from php page and send them to another page when using window.open() in javascript 从另一个窗口传递函数纯JavaScript - Pass Function from Another Window Pure JavaScript javascript将事件从一个窗口传递到另一个窗口 - javascript passing event from one window to another 获取从一个窗口到另一个窗口的值 - JavaScript - Get the value from one window to another window - JavaScript 从另一个弹出的javascript窗口打开一个新窗口 - open a new window from another pop up javascript window 从显示另一个域的另一个窗口获取文档位置 - Getting the Document Location from another Window showing another Domain JavaScript错误window.setInterval - Javascript errors window.setInterval 使用JavaScript从其他文件获取HTML - Getting HTML from another file with JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM