Accessing the content of other tabs in browser

2019-01-03 18: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 DOM (I am open to other techniques if exist).

E.g. I want to run a JavaScript 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 hyper link 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.

4条回答
该账号已被封号
2楼-- · 2019-01-03 18:38

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); };
查看更多
Deceive 欺骗
3楼-- · 2019-01-03 18:43

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.

查看更多
小情绪 Triste *
4楼-- · 2019-01-03 18:48

You could use HTML5 cross-window messaging...but that's kinda cutting edge.

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

查看更多
5楼-- · 2019-01-03 18:58

Vamyip,

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.

查看更多
登录 后发表回答