Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Javascript Errors From Another Window

Tags:

javascript

I created some test code to open a new window and attempt to catch javascript errors in the new window, from the parent window. The problem is that it only works in 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:

<script>
alert('test');s //error on the s
</script>

Forefox will catch this, but Chrome and IE do not. All 3 browsers will open the new window, but the error alert only displays in Firefox. Each of the browsers' consoles also show the error.

Why don't Chrome and IE catch it?

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. This did not affect my results. Firefox was still the only one to catch the error.

<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>
like image 642
Ryan Avatar asked Aug 24 '26 02:08

Ryan


1 Answers

I did a little searching, it seems the return value of window.open() of Chrome and IE are not act as normal/original window.

It seems the reason is about security, the following post is about chrome extension, but it may help:

window.open returns undefined in chrome extension

like image 76
Oscar Avatar answered Aug 25 '26 15:08

Oscar