0

I have page that open a new window. On this window there is close button. The close button works fine.

<input type="button" onClick="javascript:window.close()" value="Close Window" class="uportal-button" style="position: absolute; right: 3px; top: 5px; z-index: 1;">

On this page, I have a html form and Save button. If you save the page and then click "close". The close button does not work.

Any suggestions on how to fix this. I tried suggestions from window.close and self.close do not close the window in Chrome article but did not work

Mister Jojo
  • 20,093
  • 6
  • 21
  • 40
Meera
  • 455
  • 1
  • 4
  • 12
  • ["This method can only be called on windows that were opened by a script using the Window.open() method."](https://developer.mozilla.org/en-US/docs/Web/API/Window/close) Is this the case? – Andy Jun 08 '21 at 23:13
  • When you submit the form does it reload a new page and is it a page you control the code in? – charlietfl Jun 08 '21 at 23:18
  • it looks like when I save the page, its not the page that opened the window. Is there a way I can write a function to loop through all the parent/child window and close them. – Meera Jun 08 '21 at 23:47
  • Tried the following also as workaround but getting "Uncaught TypeError: Cannot read property 'postMessage' of null" – Meera Jun 08 '21 at 23:49
  • I have an app that opens a window to do oauth through another service. When other service redirects back to my site I just call window.close() in script tag. I also keep checking in my app that opened the window for `newWindow.closed` so I know when it is closed. – charlietfl Jun 09 '21 at 00:02

1 Answers1

0

The Window.close() method closes the current window, or the window on which it was called.

This method can only be called on windows that were opened by a script using the Window.open() method. If the window was not opened by a script, an error similar to this one appears in the console: Scripts may not close windows that were not opened by script.

See documentation here for more details.

Dharman
  • 30,962
  • 25
  • 85
  • 135
snd
  • 133
  • 6
  • We don't know if this answers the question which is why I asked the in the comment section if this was the case. At the moment this is just supposition. – Andy Jun 08 '21 at 23:16
  • OK I see. I didn't understand well. Thank – snd Jun 08 '21 at 23:34