jQuery - 如何在弹出窗口中设置对父页面的回调
当我打开弹出窗口时,我希望能够设置弹出窗口可以调用的一些回调。 所以基本上我有两个页面,其中包含这样的代码:
*****popup.html*******
var callBack;
function OnSaveClick()
{
if (callBack)
callBack();
}
**********************************************
********popupOpener.html*************
function callBackHandler()
{
//some code here
}
function OpenPopup()
{
var p = window.open("popup.html");
p.callBack = callBackHandler;
return false;
}
问题是,当 popup.html 的 DOM 加载时 var callBack 会重置,并且直到打开器上的 OpenPopup() 完成后才会加载。 因此,下一个最好的办法是在 popup.html 的就绪事件中设置回调。但我希望能够将事件处理程序附加到 popupOpener.html 中 popup.html 的就绪事件。 所以 OpenPopup 函数现在看起来像这样:
var p;
function OpenPopup()
{
p = window.open("popup.html");
$(p).ready(hookCallBack) //doesn't work
//or $(p.document).ready(hookCallBack) //doesn't work
return false;
}
function hookCallBack()
{
p.callBack = callBackHandler;
}
但是 hookCallBack() 在 $(p).ready(hookCallBack) 之后立即执行,而不是在 popup.html 的 DOM 准备好时执行。还有其他方法可以做到这一点吗?
When i open a popup, i want to be able to set few callbacks that the popup can call.
So basically i'd have two pages with code like this:
*****popup.html*******
var callBack;
function OnSaveClick()
{
if (callBack)
callBack();
}
**********************************************
********popupOpener.html*************
function callBackHandler()
{
//some code here
}
function OpenPopup()
{
var p = window.open("popup.html");
p.callBack = callBackHandler;
return false;
}
The problem with this is that var callBack gets reset when the DOM of popup.html loads and it doesn't load until OpenPopup() on the opener completes.
So the next best thing would be to set the callBack in the ready event of popup.html. But i want to be able to attach an event handler to the ready event of popup.html in popupOpener.html.
So the OpenPopup function would now look something like this:
var p;
function OpenPopup()
{
p = window.open("popup.html");
$(p).ready(hookCallBack) //doesn't work
//or $(p.document).ready(hookCallBack) //doesn't work
return false;
}
function hookCallBack()
{
p.callBack = callBackHandler;
}
But hookCallBack() executes immediately after $(p).ready(hookCallBack) and NOT when the DOM of popup.html is ready. Is there any other way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
请参阅我的问题: 获取弹出窗口的 DOM 元素以进行 jQuery 操作< /a> 了解如何在加载后控制弹出窗口的答案
因此您可以执行类似@MattBall的答案的操作:
popupOpener.html:
因此要在页面加载时加载弹出窗口:
这是一个演示: http://jsfiddle.net/EJasA/
See my question: Get DOM elements of a popup for jQuery manipulation for an answer of how to control the popup after it is loaded
So you can do something like @MattBall's answer:
popupOpener.html:
So to load the popup on page load:
Here is a demo: http://jsfiddle.net/EJasA/
在弹出页面中引用“opener”来获取创建弹出窗口的页面:
例如:
请参阅 http://www.webreference.com/js/tutorial1/opener.html
in your popup page refer to "opener" to get the page which created the popup:
so for example:
see http://www.webreference.com/js/tutorial1/opener.html
回调函数是字符串
callback_function is string