18

I am using Mozilla Firefox and I am trying to figure out a way to access the content of other tabs in the same window using JavaScript and the DOM (I am open to other techniques if exist).

E.g., I want to run JavaScript code in tab1 which can find the title of some other tab. Basically I need this so that I can identify a tab which has opened due an href in my current page without using window.open method. All I want is a simple hyperlink which opens a page belonging to the same domain as the current page (the page should be opened in a new tab). Now I want to be able to access this new tab from the current tab.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
vamyip
  • 1,161
  • 1
  • 10
  • 35

5 Answers5

17

Whilst you can easily open a new window using JavaScript, I'm sure that is as far as it goes. From a security point of view you wouldn't want JavaScript in one tab being able to query / access the DOM in another tab. Any site would then be able to gain access to your bank account details, etc. if both sites were opened in separate tabs.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Paul Hadfield
  • 6,088
  • 2
  • 35
  • 56
  • 11
    yes..I know that. But, as I mentioned earlier, the new tab/window will belong to the same domain, so I won't possibly "steal" bank account details from my own website. Anyways thanks for your prompt reply guys...but need to find a workaround for this. - Thanks again – vamyip Jul 09 '10 at 04:50
  • Can you create a similar effect by tracking open tabs server-side? E.g. suppose you have intentionally coded certain links to open in a new tab, then you know the user has two tabs open. Further, if you have client side js (ajax) "checking in" every few seconds from both tabs, inter-tab communication could be relayed through the server. E.g. perhaps tab 1 is editing "Zoo" object and changes its name from "Boston" to "Denver". Tab 2 is "Boston Zoo Animals" where you can change the animals in the zoo. Tab 2 gets updated to "Denver Zoo Animals" with ajax update. – Buttle Butkus Jul 15 '17 at 00:44
12

You can access the new window/tab if it was opened with JavaScript and the page indeed is in the same domain.

You can open the window/tab like so

var win = window.open("/path_to_page");

Then you'll have to wait for the page to load before you can access e.g. the title.

win.onload = function(){ alert(win.document.title); };
livedo
  • 1,069
  • 9
  • 6
  • 1
    Just make sure both pages have the same origin or you will get the following error: `Blocked a frame with origin "http://x" from accessing a frame with origin "http://y". Protocols, domains, and ports must match.` – supercoco Jun 05 '14 at 15:47
11

You could use HTML5 cross-window messaging (archive.org link...but that's kind of cutting edge.

Even in that case, you'd probably need to hijack the <a> tag 'click' event with JavaScript and open the window yourself so that you'd have access to the new window object for posting messages.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sje397
  • 41,293
  • 8
  • 87
  • 103
  • 4
    Looks like we made a lot of steps forward in the last six years :) http://caniuse.com/#feat=x-doc-messaging – Loupax Dec 06 '16 at 11:01
  • @Loupax As long as you're [not supporting IE](https://stackoverflow.com/questions/16226924/is-cross-origin-postmessage-broken-in-ie10). – ruffin Mar 19 '21 at 17:13
10

Try setting a cookie which is accessible to any page in the same domain. On other pages, use a JavaScript timer to check if the cookie value has changed and when it has you can use its value and take an action.

It worked for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Shane
  • 303
  • 2
  • 9
-1

Well, this would not be possible, you could try

<a target="_blank" rel="opener" href="about:blank"></a>

This makes a link that opens an about:blank, this will have the same domain as the page that opened It because of the Same-Origen-policy.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 21 '23 at 22:57