Question

[Solved] window.close() doesn’t work – Scripts may close only the windows that were opened by it

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

Solution #1:

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.

Respondent: somethinghere

Solution #2:

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 to test that controller. Then I have discovered that the problem occurs always which 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.

Respondent: mayconfsbrito

Solution #3:

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();
Respondent: andrex

Solution #4:

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();
Respondent: Hayk

Solution #5:

Messy but it worked for me.

window.close();
setTimeout(() => {window.close();}, 2000);
setTimeout(() => {window.close();}, 4000);

Respondent: shasabbir

Solution #6:

The below code worked for me 🙂

window.open('your current page URL', '_self', '');
window.close();
Respondent: DfrDkn

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy