jQuery - 如何在弹出窗口中设置对父页面的回调

发布于 2024-11-08 16:08:51 字数 1009 浏览 0 评论 0原文

当我打开弹出窗口时,我希望能够设置弹出窗口可以调用的一些回调。 所以基本上我有两个页面,其中包含这样的代码:

*****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 技术交流群。

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

发布评论

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

评论(3

默嘫て 2024-11-15 16:08:51

请参阅我的问题: 获取弹出窗口的 DOM 元素以进行 jQuery 操作< /a> 了解如何在加载后控制弹出窗口的答案

因此您可以执行类似@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();
};

这是一个演示: 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:

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');
        }
    }

So to load the popup on page load:

window.onload = function(){
    opener.some_function();
};

Here is a demo: http://jsfiddle.net/EJasA/

坐在坟头思考人生 2024-11-15 16:08:51

在弹出页面中引用“opener”来获取创建弹出窗口的页面:

例如:

function OnSaveClick()
{
  if (callBack) //just have callback as a boolean field
      opener.top.frameFocus.callbackfunc();
}

请参阅 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:

function OnSaveClick()
{
  if (callBack) //just have callback as a boolean field
      opener.top.frameFocus.callbackfunc();
}

see http://www.webreference.com/js/tutorial1/opener.html

开始看清了 2024-11-15 16:08:51
if(callback_function) {
   eval('window.opener.' + callbak_function)(name, age, address);
}

回调函数是字符串

if(callback_function) {
   eval('window.opener.' + callbak_function)(name, age, address);
}

callback_function is string

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