0

I tried to close a window using window.close() when user clicks on cancel button, but window not closing.I am getting the following message in console

scripts may close only the windows that were opened by it. 
Graham
  • 7,431
  • 18
  • 59
  • 84
vishnu
  • 4,377
  • 15
  • 52
  • 89
  • 1
    You will have to come up with a way to open the window with javascript, then you can close it with javascript - this seems to work https://jeffclayton.wordpress.com/2015/02/13/javascript-window-close-method-that-still-works/ – Funk Doc May 07 '19 at 18:26
  • 1
    Possible duplicate of [window.close() doesn't work - Scripts may close only the windows that were opened by it](https://stackoverflow.com/questions/25937212/window-close-doesnt-work-scripts-may-close-only-the-windows-that-were-opene) – saitama May 07 '19 at 18:42

1 Answers1

0

this question has already been answered HERE

This method window.close() is only allowed to be called for windows that were opened by a script using the window.open() method.

It's a security to prevent a website taking control of your browser and closing windows.

You can try this. As it has been used here Reference Link

function CloseWithWindowOpenTrick()
{
    var objWindow = window.open(location.href, "_self");
    objWindow.close();
}
saitama
  • 699
  • 1
  • 9
  • 21
  • Actually, it is a duplicate from [here](https://stackoverflow.com/questions/19761241/window-close-and-self-close-do-not-close-the-window-in-chrome). – k3llydev May 07 '19 at 18:57
  • can try this also `open(location, '_self').close();` [See Its also answered here](https://stackoverflow.com/questions/19761241/window-close-and-self-close-do-not-close-the-window-in-chrome) – saitama May 07 '19 at 18:58