0

I am trying to close Chrome/Safari browser tab using javascript. I tried all the below code. But nothing is working.

open('about:blank', '_self').close(); - This opens up a new tab, and the user is able to browse back. Which is not required.
open(location, '_self').close(); - Scripts may close only the windows that were opened by it.
window.open('','_parent','');
window.close(); - Not working

Is it a browser restriction?

Ayyappan Subramanian
  • 5,348
  • 1
  • 22
  • 44
  • Possible duplicate of [How to close current tab in a browser window?](https://stackoverflow.com/questions/2076299/how-to-close-current-tab-in-a-browser-window) – Taki Oct 02 '19 at 19:02
  • https://stackoverflow.com/questions/2076299/how-to-close-current-tab-in-a-browser-window#comment2006744_2076307 – Taki Oct 02 '19 at 19:03
  • 1
    If you did not create it with JavaScript, you do not have the right to close it. – epascarello Oct 02 '19 at 19:11
  • https://developer.mozilla.org/en-US/docs/Web/API/Window/close#Closing_the_current_window – Hitech Hitesh Oct 02 '19 at 19:34

1 Answers1

0

The window.close doesn't work because it doesn't know what to close try this

//Global var to store a reference to the opened window
var openedWindow;

function openWindow() {
  openedWindow = window.open('moreinfo.htm');
}

function closeOpenedWindow() {
  openedWindow.close();
}

Hitech Hitesh
  • 1,641
  • 1
  • 9
  • 18