window.close() doesn't work - Scripts may close only the windows that were opened by it
JavascriptWindowKeyboard ShortcutsShortcutJavascript Problem Overview
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();
Javascript Solutions
Solution 1 - Javascript
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.
Solution 2 - Javascript
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.
Solution 3 - Javascript
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();
Solution 4 - Javascript
There is no permanent fix even though someone answered this the code still breaks adventually its basically just a useless java script feature
Solution 5 - Javascript
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
Solution 6 - Javascript
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();
Solution 7 - Javascript
Working Solution 2022
const closeWindow = () => {
window.open(location.href, "_self", "");
window.close()
}
How it worked ?
- Basically window.close() only work if any window launched by window.open()
- Here we firstly redirect to same url then close and it worked :)
Solution 8 - Javascript
Messy but it worked for me.
window.close();
setTimeout(() => {window.close();}, 2000);
setTimeout(() => {window.close();}, 4000);
Solution 9 - Javascript
The below code worked for me :)
window.open('your current page URL', '_self', '');
window.close();