window.close() doesn't work - Scripts may close only the windows that were opened by it

mayconfsbrito picture mayconfsbrito · Sep 19, 2014 · Viewed 117.9k times · Source

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();

Answer

somethinghere picture somethinghere · Sep 19, 2014

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.