40

I'm trying to close a child window with JavaScript, and in Firefox everything works fine but in Chrome the window doesn't close.

Here is what I'm using

$(document).ready(function() {
    if (window.opener && !window.opener.closed)
        window.opener.location = "http://www.website.com"
    window.close();
});

I tried a suggestion on google, but to no avail.

Anyone having a similar issue or know of a work-around?

Null
  • 1,950
  • 9
  • 30
  • 33
mrpatg
  • 10,001
  • 42
  • 110
  • 169

6 Answers6

86

I know this question is old, but I ran into the same problem. This worked for me:

window.open('', '_self', ''); //bug fix
window.close();
Warren Benedetto
  • 2,478
  • 2
  • 22
  • 25
  • @Long I think because the bug fix crashed with first window.open() so it should be delayed. @Warren Thanks. – Jeaf Gilbert Jun 24 '10 at 03:33
  • 6
    How future-proof is this solution? Does anyone know why this works? I thought the general rule was that you could not close a window that was not opened with JavaScript. – Walter Stabosz Apr 03 '12 at 18:49
  • 1
    @WalterStabosz If you directly use window.close() then it will not work because only tab that opened from javascript are allowed to close. So we open some blank url in same window and then try to close it via javascript and it works!! – Parixit Jul 02 '13 at 12:24
  • I can't get this to work; I've opened the child window using LyteBox, and I can "post back" to the main window using parent.document, but then it just ignores the window.close - which is odd, as the child window was opened using JavasScript in the first place. – Marc Wilson Nov 02 '13 at 02:06
  • I can't seem to get it to work anymore, unless I serve the page locally. – cjm Jul 27 '16 at 06:17
10

If previously you open some other window by window.open()

This don't work:

window.open(...)
window.open('', '_self', '');
window.close();

But work:

window.open(...);
setTimeout(function(){
    window.open('', '_self', '');
    window.close();
}, 100);
Amaan warsi
  • 184
  • 4
  • 18
2

Something like this should also work:

setTimeout(function() {
    window.close();
},50);
Amaan warsi
  • 184
  • 4
  • 18
Max Kramnik
  • 189
  • 1
  • 8
1

I think it's working in Chrome Kiosk ( Fullscreen ) mode. Tried successfully.

nembleton
  • 2,392
  • 1
  • 18
  • 20
1

top.window.close() works for me. Tested on IE, FF, Chrome, Safari and Opera.

Ron
  • 245
  • 3
  • 9
0

This worked for me

var win = window.open("about:blank", "_self");
win.close();
Amaan warsi
  • 184
  • 4
  • 18
Gerardo Abdo
  • 1,150
  • 3
  • 10
  • 16