当我打开弹出窗口时,我希望能够设置弹出窗口可以调用的几个回调。 所以基本上我有两页代码如下:
*****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的ready事件中设置callBack。但我希望能够在popupOpener.html中将事件处理程序附加到popup.html的ready事件中。 所以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准备就绪时则不执行。还有其他办法吗?
答案 0 :(得分:2)
请参阅我的问题:Get DOM elements of a popup for jQuery manipulation,了解如何在加载后控制弹出窗口
所以你可以做@ MattBall的回答:
popupOpener.html:
var opener = {
popup: null,
newPopup: function(windowsname){
this.popup = window.open(windowsname);
var self = this;
this.popup.onload = function ()
{
var doc = this.document,
script = doc.createElement('script');
script.src = 'http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js';
script.onload = function ()
{
function setup()
{
//something to do on the popup
}
script = doc.createElement('script');
script.textContent = "(" + setup.toString() + ")();";
doc.body.appendChild(script);
};
doc.head.appendChild(script);
};
},
some_function: function(){
//calling it here:
this.newPopup('popup.html');
}
}
所以要在页面加载时加载弹出窗口:
window.onload = function(){
opener.some_function();
};
答案 1 :(得分:0)
引用“opener”来获取创建弹出窗口的页面:
所以例如:
function OnSaveClick()
{
if (callBack) //just have callback as a boolean field
opener.top.frameFocus.callbackfunc();
}
答案 2 :(得分:0)
if(callback_function) {
eval('window.opener.' + callbak_function)(name, age, address);
}
callback_function是字符串