Any way to identify browser tab in JavaScript?

后端 未结 9 626
孤独总比滥情好
孤独总比滥情好 2020-11-27 05:17

I need to be able to identify what tab I am in within the browser. Isn\'t there some bit of information I can get from the browser to identify the tab? I don\'t need to kn

9条回答
  •  独厮守ぢ
    2020-11-27 05:38

    Since I don't have find no simple Javascript function as windows.currentTabIndex, I have written some lines of Javascript to fix an ID on each browser tabs when they are loaded.

    function defineTabID()
        {
        var iPageTabID = sessionStorage.getItem("tabID");
          // if it is the first time that this page is loaded
        if (iPageTabID == null)
            {
            var iLocalTabID = localStorage.getItem("tabID");
              // if tabID is not yet defined in localStorage it is initialized to 1
              // else tabId counter is increment by 1
            var iPageTabID = (iLocalTabID == null) ? 1 : Number(iLocalTabID) + 1;
              // new computed value are saved in localStorage and in sessionStorage
            localStorage.setItem("tabID",iPageTabID);
            sessionStorage.setItem("tabID",iPageTabID);
            }
        }
    

    This code save last tab's index in localStorage to update current value for new tabs and in sessionStorage to fix page's id !

    I must call defineTabId() function in each pages of my application. Since I develop using a JSF templates, this is only defined in only one place :-)

提交回复
热议问题