0

Is it possible to close a different browser tab than the active one from another HTML page with Javascript?

I ask because I am building a virtual assistant in the browser. This assistant will open new tabs, at which point the current tab would be something like a Google search. If I could ask the virtual assistant to close this current tab, I would be returned to the virtual assistant browser window.

Anthony
  • 13,434
  • 14
  • 60
  • 80
  • 2
    It should be possible, but javascript can only close windows it has opened, not random windows the user has opened. – adeneo Nov 29 '15 at 22:06
  • 2
    Not sure why the thumb down. I searched as far and wide as I could for the answer, asking it on S.O. was not my first choice. – Anthony Nov 29 '15 at 22:12

1 Answers1

5

If you opened the browser tab using window.open and if you have the reference to it, and it is from the same domain, yes, you can close it.

var myWin = null;

function openWin () {
  myWin = window.open("/hello.htm", "_blank");
}

function closeWin () {
  myWin.close();
}
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
  • 2
    Please close the dupes like this, if you cannot add to existing answers, don't answer such questions. – Tushar Nov 30 '15 at 07:23