0

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.

Harsh Shankar
  • 506
  • 5
  • 16
  • This is an article that has the solution to your answer: https://stackoverflow.com/questions/23306882/javascript-close-current-window#:~:text=To%20close%20your%20current%20window%20using%20JS%2C%20do,after%20running%20the%20script%20to%20open%20the%20child. – cs641311 Mar 15 '21 at 15:42
  • @cs641311 tried that approach already. On close, error is "Scripts may close only the windows that were opened by them." – Harsh Shankar Mar 15 '21 at 15:45
  • JavaScript can not close tabs it did not create so unless you created it. Most you can probably do is redirect a page if you detect this case. There are also tons of other edge cases. Such a weird requirement. – epascarello Mar 15 '21 at 15:45
  • @epascarello Not so weird requirement. Page is launched when the device reaches near an NFC tag. tags keep comming and user does not want to keep closing tabs since this is factory and closing may distract the user . So they want tabs to be closed as soon as new tab is opened – Harsh Shankar Mar 15 '21 at 15:51
  • What opens up these tabs? Seems like that would be the place to look. – epascarello Mar 15 '21 at 16:01
  • @epascarello websites can be launched with nfc tag scan. It is something like https://www.youtube.com/watch?v=ZClH8zJBEEk – Harsh Shankar Mar 15 '21 at 17:31
  • I think you are basically out of luck from a JavaScript standpoint because only way window.close works is if you opened it up with window.open(). Maybe it can be done with an extension that runs a check. – epascarello Mar 15 '21 at 17:43

1 Answers1

1

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.
shtse8
  • 1,092
  • 12
  • 20
  • 1
    Have simpler method to find if this is an older page. On load, read the cookie, added 1 and saved in a variable as instance index and updated the cookie with instance index. Ran a timer to keep checking the cooking value with this variable. if not same, close tab which is not working. So, no server calls needed – Harsh Shankar Mar 15 '21 at 15:54
  • yes, you are right if the client support cookies or local storage. My approach can work without cookies or local storage but a server is needed. I assume you are building web application, server is a must. – shtse8 Mar 15 '21 at 16:05
  • Server is there but keep calling server is not needed if can find out without approaching to server. – Harsh Shankar Mar 15 '21 at 16:08