将所有功能移至子窗口

发布于 2024-12-13 06:22:19 字数 256 浏览 4 评论 0原文

如果我这样做:

var new_win = window.open();

如何才能使可以在父窗口中使用的所有函数现在都可以在子窗口中使用(new_win)?

不想想做:

var fun1 = window.opener.fun1;
var fun2 = window.opener.fun2;
...

If I do this:

var new_win = window.open();

How do I make it so that all of the functions that could be used in the parent window can now be used in the child window (new_win)?

I do not want to do:

var fun1 = window.opener.fun1;
var fun2 = window.opener.fun2;
...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄意 2024-12-20 06:22:19

请注意,接下来的内容是危险的、肮脏的、混乱的、M$ 级的黑客攻击。我完全意识到这一点,但它(理论上)做到了@Neal 想要的。 (我什至都​​有点害怕发布它,我完全希望有人反对)

var i, w = window.opener;
for (i in w)
{
    if (w.hasOwnProperty(i) && !window.hasOwnProperty(i) && typeof w[i] === 'function')
    {
        window[i] = w[i];
    }
}

鉴于范围问题,我确定我们必须使用 .bindFunction.bind 的 MDN 条目 这对于某些浏览器来说是必需的。

请注意,在使用 .bind 之前,代码必须检查该属性是否是一个函数。我已将其与 hasOwnProperty 检查一起完成,但如果您希望传输值以及函数,则可能需要在其自己的 if 语句中执行此操作。

var i, w = window.opener;
for (i in w)
{
    if (w.hasOwnProperty(i) && !window.hasOwnProperty(i) && typeof w[i] === 'function')
    {
        window[i] = w[i].bind(window);
    }
}

Please note that what follows is a dangerous, dirty, messy, M$-level hack. I am fully aware of this, but it (theoretically) does what @Neal wants. (I'm a little scared to even post it, and I fully expect downvotes)

var i, w = window.opener;
for (i in w)
{
    if (w.hasOwnProperty(i) && !window.hasOwnProperty(i) && typeof w[i] === 'function')
    {
        window[i] = w[i];
    }
}

In light of the scope issues, I've determined that we must use .bind. There is a shim at the MDN Entry for Function.bind which will be necessary for certain browsers.

Please note that before using .bind, the code must check to see if the property is a function. I have done this along with the hasOwnProperty checks, but if you wish to transfer values as well as functions, you may want to do this within its own if statement.

var i, w = window.opener;
for (i in w)
{
    if (w.hasOwnProperty(i) && !window.hasOwnProperty(i) && typeof w[i] === 'function')
    {
        window[i] = w[i].bind(window);
    }
}
韬韬不绝 2024-12-20 06:22:19

首先,我将创建一个命名空间(有助于避免函数冲突),然后我将在弹出窗口中引用它。

父窗口:

MyNameSpace = {
      // put functions, classes or whatever you want in here.
};

在弹出窗口中:

MyNameSpace = window.opener.MyNameSpace;

您所要求的唯一潜在问题是您调用的函数是否试图引用窗口对象。我会将窗口对象传递给任何操作窗口的函数。

例如

function (arg1, arg2, argn, windowHandle) {
      windowHandle = windowHandle || self;

      // do some stuff.

}

First off, I would create a namespace (helps avoid function collisions) and then I would just reference it in the popup window.

parent window:

MyNameSpace = {
      // put functions, classes or whatever you want in here.
};

in the pop-up window:

MyNameSpace = window.opener.MyNameSpace;

The only issue potentially with what you're asking for is if the function you call is trying to reference the window object. I would pass a window object to any functions that manipulate a window.

e.g.

function (arg1, arg2, argn, windowHandle) {
      windowHandle = windowHandle || self;

      // do some stuff.

}
度的依靠╰つ 2024-12-20 06:22:19

您可以创建函数的新实例并传递函数名称和可选的 argN,如下所示

var parentFunction = new Function("name", "args", "return (window.opener != null) ?  opener.window[name](args) : false");

parentFunction('notification','waring, red, 2');

You could create an new instance of a function and pass the function name and optional argNs something like as follows

var parentFunction = new Function("name", "args", "return (window.opener != null) ?  opener.window[name](args) : false");

parentFunction('notification','waring, red, 2');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文