I'm debugging issues with running our web app in multiple browser tabs and have added a client log using log4javascript. I'm now trying to identify which log line is written by which browser tab, so I want to add a unique tab ID to each log line.
What this boils down to is that I need a way to identify that a browser tab is "new" and needs to receive a new tab ID. Tabs that have already received a tab ID should keep this ID.
But I have not found a way to store such an ID in a way that:
Here is what I've tried:
Store the tab ID in the session storage and generate a new ID if a tab's session storage does not yet contain such an ID.
This works quite well in most cases. However, there are cases where a new tab starts out with a full copy of the session storage of the tab its been opened from. This seems to be the case when the new tab is being opened by clicking a hyperlink that references a new window target, or if the tab is duplicated. In this case we cannot differentiate between both tabs, so that both tabs end up using the same tab ID.
Attach the tab ID to an HTML element using jQuery.data() and generate a new ID if the HTML element's data does not yet contain such an ID.
This was stupid because it obviously does not survive tab reloads.
Store the tab ID in the window.name of the iframe that contains our application and generate a new ID if the window.name does not yet contain such an ID.
This doesn't work because the embedding application that generates the HTML iframe tag (and which we cannot change) sets the iframe name, so the tab ID is lost after each tab reload.
Store the tab ID in the window.top.name and generate a new ID if the window.top.name does not yet contain such an ID.
This doesn't work because we cannot set the window.top.name due to cross site security constraints (the embedding application is hosted in a different domain than the contents of our iframe).
Embed yet another iframe within the iframe that contains our application, store the tab ID in the window.name of that iframe and generate a new ID if the inner iframe's window.name does not yet contain such an ID.
This does not work because even though we do not explicitly set the inner iframe's name when opening it, the iframe's name is reset to the empty string upon tab reload. This is how we open the inner iframe:
<iframe id="iframe2" src="index.jsp"></iframe>
I understand why the first four options didn't work. I'm not sure why the fifth option did not work as well and suspect that I might make a trivial mistake.
If not, I'm wondering if there is any other way that would allow me to identify the fact that a tab is new and hence in need of receiving a new tab ID.
The ID of the tab. Tab IDs are unique within a browser session. Under some circumstances a tab may not be assigned an ID; for example, when querying foreign tabs using the sessions API, in which case a session ID may be present.
sessionStorage is similar to localStorage ; the difference is that while data in localStorage doesn't expire, data in sessionStorage is cleared when the page session ends. Whenever a document is loaded in a particular tab in the browser, a unique page session gets created and assigned to that particular tab.
You can be identified by what's called Device fingerprint, which in your case is Browser Fingerprint. While the IP address is not a good identification, a combination of other factors that your browser passes on with the request can be used to identify you uniquely or almost uniquely.
Here is simple solution to have unique id inside single tab, which is persisted across tab reloads. For another opened tab id will be different.
The key here is using sessionStorage, which keeps data only per session (opened tab).
const idFromStorage = sessionStorage.getItem("ID_KEY");
if (idFromStorage) {
return idFromStorage;
} else {
const id = uuid(); // generate unique UUID
sessionStorage.setItem("ID_KEY", id);
return id;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With