Accessing the content of other tabs in browser

让人想犯罪 __ 提交于 2019-12-17 03:18:54

问题


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.


回答1:


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.




回答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.




回答3:


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.




回答4:


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


来源:https://stackoverflow.com/questions/3203530/accessing-the-content-of-other-tabs-in-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!