check if a browser tab is already open so I don't make extra tabs

23,966

Solution 1

window.open has the following parameters: var tab = window.open(url, name, specs, replace) As long as you use the same name the url will be loaded into that window/tab.

If you wanted to keep the descriptor/reference (tab above), that window.open returns, once the user refreshes the page, that reference is lost.

Solution 2

I think your best bet could be session storage / local storage, but it works only in newer browsers.

Share:
23,966
BobB
Author by

BobB

Updated on July 21, 2022

Comments

  • BobB
    BobB almost 2 years

    When a user adds an item to our shopping cart it opens our store in a new tab. Different websites oddly enough.

    I would like to check if the tab is already open and then repopulate it it with the second item instead of opening another tab with the updated cart.

    Is there a way to check this with js? I imagine I can track that we opened the tab but I don't see how I can confirm that it wasn't closed in the time between adding items to the cart without doing some ajax requests pinging both pages etc. Which seems like overkill.

    So simply how do you check if a browser tab is already open?

    Edited with a solution: First:

    var tab = window.open('http://google.com','MyTab');
    

    Then:

    if(tab) {
      var tab = window.open('http://yahoo.com','MyTab');
    }
    
  • BobB
    BobB over 11 years
    Ideally I could persist this beyond the page that the user is on as products are on different pages on the site.
  • BobB
    BobB over 11 years
    I think you are right if I want to persist beyond the page the user is on. Thanks
  • J. Steen
    J. Steen over 11 years
    @BobB If this answers your question, click the tick-mark on the left side to mark it as such. =)
  • BobB
    BobB over 11 years
    It mostly answers it so I clicked it but I am still researching persisting beyond the page that opened the tab. I will experiment with the browsers storage but I am not sure whether it can store that variable