Harsh Shankar
Harsh Shankar

Reputation: 526

Close current tab with javascript in Google Chrome

With my web application, I need to keep only a singe instance of the page at once. If new instance is opened in a new tab, I need to close the previous one. I have seen many posts related to this and all of them are saying that I can only close tabs in Chrome that are opened programmatically and by created by itself but not the current tab. I am aware of it being a security threat but this is the customer's requirement. Client browser settings are controlable and I can do any settings as needed. So, I am looking a way to go around this security policy.

Upvotes: 0

Views: 681

Answers (1)

shtse8
shtse8

Reputation: 1365

You only can close the page you open. but you can close another tab.

But you can still achieve what you want. Communicate to your server and check if the same client open two tabs by checking their token stored in their page. If yes, close one of them (you can only close itself), if you are failed to close itself because of some security reason, then just turn the whole page white.

  1. ping to server with token when page onload and keep heartbeat per event n seconds (normally, 5s - 60s is fine).
  2. server stores generate a tab connection token and store the session. reply back to the client. You can add some logic here, eg. close tab.
  3. client receive the response, process the action, eg. close tab. If failed to close, put a overlay on top or remove all elements on page, or even redirect to another page.
  4. remember to remove timed out session in servers.

Upvotes: 1

Related Questions