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. }); }
source share