0

I am trying to detect whether the current page is already opened in a different tab or not.

I tried using window.open(), but I get as return the current window itself, not the supposedly other window of same address. It will always return true.

So say, if 'mysite.com' is already opened, when the user opens a new window with 'mysite.com', I shall detect that, focus the other window and close the current one.

Would love to hear any ideas, or whether this is possible. I'm trying to capture all links into a single tab.

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164
ArtPulse
  • 365
  • 4
  • 16
  • I believe this is possible but you will need some help using PHP. Related question: http://stackoverflow.com/questions/5574970/how-to-detect-if-user-open-two-tabs-for-same-session – aug Jul 30 '13 at 06:08
  • What if the user has two laptops, or uses netscape and IE, or... ? What if the user left himself logged in at work and now he is at home, or vice-versa? The point is that micromanaging the user can get a bit tiresome, even futile. – Paul Jul 30 '13 at 06:13
  • I'm pretty sure that it's not possible to close one tab and activate another _specific_ tab by JS code. (Imagine how annoying it would be if ads were able to activate their own tabs while you're browsing in another tab or window.) But you can at least use `window.close()` to close a tab or window - but the browser will maybe ask the user whether to close the tab/window if it wasn't opened by code, too. (Imagine the fun you'd have with `` or ads which are able to close any window...) – fero Jul 30 '13 at 08:27
  • It's not about micromanaging. Pretend I have a tabbed based navigation within my WebApp, and I want all links to it to simply "add another tab" within my WebApp. They are not actually tabs, but gives you a sense of why I want this =P. But I guess you are right, maybe I can't find a duplicate and focus it... – ArtPulse Jul 30 '13 at 16:58

2 Answers2

1

You can use localStorage events to communicate between different tabs and therefore detect if a page is already opened. Check this answer: https://stackoverflow.com/a/14792159/60745

Community
  • 1
  • 1
flicher
  • 81
  • 4
  • True, actually I've already wrote some code that allows objects within different windows of the same app to talk to each other. However that solves half of the problem. I would still need the original window to regain focus and have the secondly open window to close... – ArtPulse Jul 30 '13 at 17:01
0

Even when you're only concerned with a tab in the same browser on the same machine, the problem with trying to accomplish this through pure javascript is that although you could set a cookie on each of your sites pages (window.onload) to record the user has initially visited your site, there's no safe way to ensure you remove this cookie when they leave.

Although you have the onunload & onbeforeunload events, these are not fired when you leave a site over a link, uses a browsers back button or closes the browser.

Sparko
  • 735
  • 6
  • 15