How to implement when duplicate browser tab is duplicated

I'm having problems with duplicate tabs in Chrome (session stuff), and I would like to avoid the action of duplicating tabs (or, if it’s not, duplicating). I open the tab, because it is a popup, without an address bar, there is no status bar and nothing is displayed, just a window. There is no way to duplicate a tab (open as a popup) in IE and Firefox (at least I did not find it), but in chrome it is still possible.

I also know that I cannot programmatically check if there is already a duplicate tab already open

Any idea how to approach this?

thanks!

+5
source share
3 answers

goal

Just to clarify: the goal is to detect (and close) the tab that is opened using the Chrome's Duplicate context menu.

First try

The Duplicate Tab action works almost the same as when you reload the page after the user clicks Back, then Forward, so you basically implement the version of this question :

function onLoad() { if ($('#myStateInput').val() === '') // Load with no state. $('#myStateInput').val('already loaded'); // Set state else alert("Loaded with state. (Duplicate tab or Back + Forward)"); } 

That's great and all, but you only want to find out when you “Duplicate tab”. To do this, we can put out the state in onbeforeunload . This works because onbeforeunload is only called when the user onbeforeunload "Back" or "Forward", but not when copying the tab.

Second attempt

 function onLoad() { if ($('#myStateInput').val() === '') // Load with no state. $('#myStateInput').val('already loaded'); // Set state else alert("Duplicate tab! Do something."); $(window).on('beforeunload', function() // Back or Forward buttons { $('#myStateInput').val(''); // Blank the state out. }); } 
+8
source

My application required that the user could only log in once so that I could use localStorage to reliably cache records. Signon uses localStorage flags to enforce this requirement and uses window.name to find out which user owns the tab.

The problem was that the browser tab duplication tool ruined the forced execution. The code below makes the page with the signature appear on the duplicated tab if they duplicate the signed session.

This solution has been tested in Chrome. It uses the fact that the duplicate tab does not have a name. This feature may not be available in all browsers .

The preventDuplicateTab function is called very early in the page load sequence.

 /* This prevents users from duplicating the tab. If they do it * triggers the start page which checks for duplicate userid */ function preventDuplicateTab() { if (sessionStorage.createTS) { // Chrome at least has a blank window.name and we can use this // signal this is a duplicated tab. console.log("Existing sessionStorage "+sessionStorage.createTS+" w.name="+window.name); if (!window.name) { window.name = "*ukn*"; sessionStorage.createTS = Date.now(); // treat as new window.location = "/res/Frame.htm?page=start.htm"; // force to signon screen } } else { sessionStorage.createTS = Date.now(); console.log("Create sessionStorage "+sessionStorage.createTS+" w.name="+window.name); } } 
+1
source

If you want to prevent multiple tabs (duplicate or not), you can use a combination of the accepted answer here and one of the solutions to this question: fooobar.com/questions/438592 / ...

I know that my solution with local and session storage works for me :-)

If you need to detect sessions in multiple browsers, you will need a server-side solution.

0
source

Source: https://habr.com/ru/post/1202916/


All Articles