0

I have an HTML button and I need to close the tab by clicking on it.

It must work on Chrome and Firefox and seems the common methods are not detecting from the new browser versions.

I just tried these two and not worked.

<a href="#" onclick="javascript:window.close();opener.window.focus();">C‌​lose</a>

AND

<button onclick="self.close()">Close</button> Both did not work.

Please, can you help me?

Yma
  • 23
  • 1
  • 6

1 Answers1

0

To programmatically close the tab you can use window.close() method. But, it will work only for windows which were created also programmatically. In any other case you will receive an error from the brwoser.

var newWindow = window.open(); //new tab opened
newWindow.close() // that tab closed

Also window.close() can be successfully executed in that new window. So actually to make it work you need to be sure that window with that 'close' button was created from another window programmatically and not by user navigating via some links, etc.

Artem Arkhipov
  • 7,025
  • 5
  • 30
  • 52
  • Thank you very much for the answer. So is there no any other way to close the browser tab after some activities on the page? The user will redirect to this page from another page and I need to give the feature to close it. – Yma Sep 27 '17 at 10:13
  • If it is server redirection - you will not be able to close it programmatically. But if you will implement redirection via window.open(url) - you should be able to use close it from the code. – Artem Arkhipov Sep 27 '17 at 10:15