简体   繁体   English

弹出窗口关闭时如何刷新父页面?

[英]How can I refresh parent page when a pop up is close?

I have some problems with the function to refresh the main page when a pop is closed. 关闭弹出窗口时,刷新主页的功能存在一些问题。

In my popUp page I have the following javascript 在我的弹出页面中,我有以下javascript

$(function () {        
    window.onunload = function () {
        if (window.opener && !window.opener.closed) {
            window.opener.popUpClosed();
        }
    };
});

In the main page I have the following javascript 在主页中,我有以下javascript

$(function () {
    function popUpClosed() {
        window.location.reload();
    }
});

I got an error, that the object don't support the method 'popUpClosed'. 我收到一个错误,该对象不支持方法“ popUpClosed”。

I saw this solution on this answer here in StackOverflow, but I don't know how can I fix this error. 我在StackOverflow的此答案中看到了此解决方案,但我不知道如何解决此错误。

Have you tried the next answer down: 您是否尝试过下一个答案:

var myPop = "pop up window selector"
myPop.onunload = function(){ 
  location.reload(); 
};

Source: Refresh parent window when the pop-up window is closed 来源: 弹出窗口关闭时刷新父窗口

Update 更新资料

You can get a reference to a window like this: 您可以像这样获得对窗口的引用:

var windowObjectReference = window.open(strUrl, strWindowName[, strWindowFeatures]);

Source: https://developer.mozilla.org/en/DOM/window.open 来源: https : //developer.mozilla.org/en/DOM/window.open

Update 更新资料

I just realized that window.opener.popUpClosed() is looking for the popUpClosed() function in the global scope on the main page, but it's not actually declared in the global scope, try this: 我只是意识到window.opener.popUpClosed()popUpClosed()的全局范围内正在寻找popUpClosed()函数,但实际上并未在全局范围内声明,请尝试以下操作:

var popUpClosed = function () {
    window.location.reload();
};

Removing the document.ready event handler around this will allow it to be accessible from a child document (like a popup). 删除此周围的document.ready事件处理程序将使它可以从子文档(如弹出窗口)访问。 Also notice the function is now declared like: var <name> = function () { ... }; 还要注意,该函数现在已声明为: var <name> = function () { ... }; which allows it to be accessible via the window object. 允许通过window对象访问它。

It's because window.opener does not have any method called popUpClosed . 这是因为window.opener没有任何名为popUpClosed方法。 Remove the window.opener as such: 这样删除window.opener

window.onunload = function () {
  if (window.opener && !window.opener.closed) {
    popUpClosed();
  }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM