跨浏览器选项卡调用JavaScript函数 [英] Call a JavaScript function across browser tabs

查看:117
本文介绍了跨浏览器选项卡调用JavaScript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个浏览器标签,tab1和tab2。

I have two browser tabs, tab1 and tab2.

我在tab1中有一个名为execute的函数,我想从tab2中的页面调用。

I have a function called execute in tab1, which I would like to call from the page in tab2.

这是可能的,如果是这样的话?

Is that possible and if so how?

推荐答案

JavaScript 无法 在浏览器中执行交叉表脚本(这是一种安全风险)。

JavaScript can not do cross-tab scripting in the browser (it is a security risk).

但是如果 第二个标签是从 window.open()调用打开的,并且设置了浏览器设置以便新的弹出窗口在新标签中打开 - 然后 ,tab1可以与tab2对话

If however the 2nd tab was opened from a window.open() call, and the browsers settings were set up such that new popup windows open in a new tab instead -- then yes, "tab1" can talk to "tab2"

第一个选项卡/窗口称为 opener ,因此新选项卡可以使用以下格式调用开启器上的函数:

the first tab/window is called the opener and thus the new tab can call functions on the opener using this format:

opener.doSomething();

同样,开启者可以通过使用它创建的变量来调用新选项卡/弹出窗口上的函数创建弹出窗口。

likewise, the opener can call functions on the new tab/popup, by using the variable it created when creating the popup window.

var myPopup = window.open(url, name, features);
myPopup.doStuffOnPopup();

这篇关于跨浏览器选项卡调用JavaScript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆