Accessing the content of other tabs in a browser

37,494

Solution 1

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.

Solution 2

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.

Solution 3

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); };

Solution 4

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.

Share:
37,494

Related videos on Youtube

vamyip
Author by

vamyip

I write cross-platform hybrid mobile apps.

Updated on July 09, 2022

Comments

  • vamyip
    vamyip almost 2 years

    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.

  • vamyip
    vamyip almost 14 years
    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
  • supercoco
    supercoco almost 10 years
    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.
  • Loupax
    Loupax over 7 years
    Looks like we made a lot of steps forward in the last six years :) caniuse.com/#feat=x-doc-messaging
  • Buttle Butkus
    Buttle Butkus almost 7 years
    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.
  • ruffin
    ruffin about 3 years
    @Loupax As long as you're not supporting IE.

Related