Javascript 在浏览器中访问其他选项卡的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3203530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:39:31  来源:igfitidea点击:

Accessing the content of other tabs in browser

javascriptfirefoxdomtabs

提问by vamyip

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).

我正在使用 Mozilla Firefox,我试图找出一种使用 JavaScript 和 DOM 访问同一窗口中其他选项卡内容的方法(如果存在其他技术,我愿意接受)。

E.g.I want to run a JavaScript in tab1which can find the title of some other tab. Basically I need this so that I can identify a tab which has opened due an hrefin my current page without using window.openmethod. 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.

例如,我想运行一个 JavaScript,在tab1其中可以找到其他一些选项卡的标题。基本上我需要这个,以便我可以href在不使用window.open方法的情况下识别在当前页面中打开的选项卡。我想要的只是一个简单的超链接,它打开一个与当前页面属于同一域的页面(该页面应该在新选项卡中打开)。现在我希望能够从当前选项卡访问这个新选项卡。

采纳答案by sje397

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

您可以使用HTML5 跨窗口消息传递……但这有点前沿。

Even in that case you'd probably need to hiHyman 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.

即使在这种情况下,您也可能需要使用 javascript 劫持标签“点击”事件并自己打开窗口,以便您可以访问新的窗口对象以发布消息。

回答by Paul Hadfield

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.

虽然您可以使用 javascript 轻松打开一个新窗口,但我相信就目前而言。从安全的角度来看,您不希望一个选项卡中的 Javascript 能够查询/访问另一个选项卡中的 DOM。如果两个站点都在单独的选项卡中打开,则任何站点都可以访问您的银行帐户详细信息等。

回答by livedo

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

如果新窗口/选项卡是用 JavaScript 打开的并且页面确实在同一个域中,则您可以访问它。

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

回答by Shane

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.

尝试设置一个 cookie,该 cookie 可被同一域中的任何页面访问。在其他页面上,使用 javascript 计时器检查 cookie 值是否已更改,何时更改,您可以使用其值并采取措施。

It worked for me.

它对我有用。