70

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
  • 2,085
  • 4
  • 26
  • 45
  • 1
    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

11 Answers11

26

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
  • 2,085
  • 4
  • 26
  • 45
20

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
  • 16,311
  • 2
  • 28
  • 42
  • 87
    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
  • @donatJ I do not agree with you, what if a website was opened some how, ran some scripts and then closed itself in a few seconds ... I think preventing close function is useful. – Walid Ammar Dec 05 '22 at 13:15
7

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
  • 983
  • 5
  • 14
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

kharon4
  • 13
  • 4
0

You can only close a window that your script made, so this is possible:

window.open("", "_blank", "");
window.close();

But otherwise you can change the link, like that:

location.href = "https://bruh.io";

Make sure that you have https:// Otherwise if you were in https://www.youtube.com and you run location.href = bruh.io you'll go to https://www.youtube.com/bruh.io

ZiyadCodes
  • 379
  • 3
  • 10
0

You can close the window if it was opened to users by a link like:

<А rel='opener' href='test.net '></А>

pay attention to the tag: rel='opener'

vimuth
  • 5,064
  • 33
  • 79
  • 116
0

Don't use <a href='URL' target='_blank'>open</a> in the parent to open a new window.

Use <a href='' onClick="window.open('URL'); return false">open</a> in the parent instead.

Then you can use <a href='' onClick="window.open('URL'); return false">close</a> in the children.

Works like a charm for me.

0

My workaround for internal use was to create desktop shortcuts.
Example here:

  • Google Chrome (Target)
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" https://url_of_my_app
  • Microsoft Edge (Target)
"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" https://url_of_my_app

You must allow before pop-ups and redirects from url of your app in browser settings.
So window.close(); works.
It open the pop-up and close window using:

window.open('/my_page.htm', '_blank', 'popup=true');
window.close();

I don't know if this will be patched later but it does seem to work on:

Windows 10
Google Chrome 113.0.5672.127 (Official version) 64 bits
Microsoft Edge 113.0.1774.50 (Official build) (64 bits)

Can someone test this on others OS, please?

Reference:
https://developer.mozilla.org/en-US/docs/Web/API/Window/open

Deividson Damasio
  • 431
  • 1
  • 6
  • 15
-1

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
  • 436
  • 2
  • 6
  • 30
Hayk
  • 11
-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
  • 533
  • 4
  • 9
-11

The below code worked for me :)

window.open('your current page URL', '_self', '');
window.close();
Kewin Dousse
  • 3,880
  • 2
  • 25
  • 46
DfrDkn
  • 1,270
  • 2
  • 16
  • 23