58

I'm having a problem always when I'm trying to close a window through the window.close() method of the Javascript, while the browser displays the below message on the console:

"Scripts may close only the windows that were opened by it."

This occurs in every part of the page. I can run this directly of a link, button, or a script, but this message always are displayed.

I'm tried to replace the window.close(); method for the functions (or variants of these) below, but nothing happened again:

window.open('', '_self', '');
window.close();
mayconfsbrito
  • 1,757
  • 4
  • 22
  • 38
  • You can not close windows that were not opened with `window.open()`. That window.open hack you had at the bottom used to work, but that security hole was patched by chrome. – epascarello Sep 19 '14 at 15:20

9 Answers9

19

I searched for many pages of the web through of the Google and here on the Stack Overflow, but nothing suggested resolved my problem.

After many attempts, I've changed my way of testing that controller. Then I have discovered that the problem occurs always when I reopened the page through of the Ctrl + Shift + T shortcut in Chrome. So the page ran, but without a parent window reference, and because this can't be closed.

mayconfsbrito
  • 1,757
  • 4
  • 22
  • 38
19

Error messages don't get any clearer than this:

"Scripts may close only the windows that were opened by it."

If your script did not initiate opening the window (with something like window.open), then the script in that window is not allowed to close it. Its a security to prevent a website taking control of your browser and closing windows.

somethinghere
  • 15,289
  • 2
  • 26
  • 40
  • 67
    It's also however preventing windows from closing themselves, which *doesn't* seem like a very useful security measure, and I'm relatively sure that's what 99% of the people are finding this about. – donatJ Nov 13 '17 at 22:42
  • 3
    @donatJ — It is useful as it stops a website trashing the window you are using along with it's back button which could point back to other websites. – Quentin Jul 05 '20 at 08:43
4

You can't close a current window or any window or page that is opened using '_self' But you can do this

var customWindow = window.open('', '_blank', '');
    customWindow.close();
andrex
  • 945
  • 5
  • 14
0

There is no permanent fix even though someone answered this the code still breaks adventually its basically just a useless java script feature

developer
  • 11
  • 1
0

Ran into this issue but I was using window.open('','_blank',''). The issue seems to be that the script that used window.open should also call the close function.

I found a dirty yet simple workaround that seems to get the job done -

// write a simple wrapper around window.open that allows legal close
const openWindow = () => {
  const wind =  window.open('','_blank','');
  
  // save the old close function
  const actualClose = wind.close;
  
  // Override wind.close and setup a promise that is resolved on wind.close
  const closePromise = new Promise(r=>{wind.close = ()=>{r(undefined);}});

  // Setup an async function
  // that closes the window after resolution of the above promise
  (async ()=>{
    await closePromise; // wait for promise resolution
    actualClose(); // closing the window here is legal
  })();

  return wind;
}


// call wind.close anywhere
document.getElementById('myButton').addEventListener('click', wind.close)

I don't know if this somehow defeats some security feature or if this will be patched later but it does seem to work on chrome version 101.0.4951

gudCoder
  • 1
  • 2
-2

The windows object has a windows field in which it is cloned and stores the date of the open window, close should be called on this field:

window.open("", '_self').window.close();
Cananau Cristian
  • 372
  • 2
  • 6
  • 28
Hayk
  • 1
-4

Working Solution 2022

const closeWindow = () => {
 window.open(location.href, "_self", "");
 window.close()
}

How it worked ?

  1. Basically window.close() only work if any window launched by window.open()
  2. Here we firstly redirect to same url then close and it worked :)
Shivam Gupta
  • 465
  • 4
  • 9
-6

Messy but it worked for me.

window.close();
setTimeout(() => {window.close();}, 2000);
setTimeout(() => {window.close();}, 4000);
shasabbir
  • 7
  • 2
-12

The below code worked for me :)

window.open('your current page URL', '_self', '');
window.close();
Kewin Dousse
  • 3,643
  • 2
  • 24
  • 45
DfrDkn
  • 1,110
  • 1
  • 15
  • 21