将所有功能移至子窗口
如果我这样做:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请注意,接下来的内容是危险的、肮脏的、混乱的、M$ 级的黑客攻击。我完全意识到这一点,但它(理论上)做到了@Neal 想要的。 (我什至都有点害怕发布它,我完全希望有人反对)
鉴于范围问题,我确定我们必须使用
.bind
。 Function.bind 的 MDN 条目 这对于某些浏览器来说是必需的。请注意,在使用
.bind
之前,代码必须检查该属性是否是一个函数。我已将其与hasOwnProperty
检查一起完成,但如果您希望传输值以及函数,则可能需要在其自己的if
语句中执行此操作。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)
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 thehasOwnProperty
checks, but if you wish to transfer values as well as functions, you may want to do this within its ownif
statement.首先,我将创建一个命名空间(有助于避免函数冲突),然后我将在弹出窗口中引用它。
父窗口:
在弹出窗口中:
您所要求的唯一潜在问题是您调用的函数是否试图引用窗口对象。我会将窗口对象传递给任何操作窗口的函数。
例如
First off, I would create a namespace (helps avoid function collisions) and then I would just reference it in the popup window.
parent window:
in the pop-up window:
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.
您可以创建函数的新实例并传递函数名称和可选的 argN,如下所示
You could create an new instance of a function and pass the function name and optional argNs something like as follows