开发者

Accessing the content of other tabs in a browser

开发者 https://www.devze.com 2023-01-06 15:37 出处:网络
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).

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.


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.


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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消