简体   繁体   English

如何在JavaScript的子窗口关闭时调用父窗口函数?

[英]How to call parent window function on close of child window in javascript?

//Parent page javascript

function DoABC()
{
    //my logic goes here
}

function btnClick() //This function gets fired on the click event of a button which will open a child window now
{
    window.open("childpage.html", "_blank", "height=400, width=550, status=yes, toolbar=no, menubar=no, location=no, addressbar=no"); 
}

Now when I close this child form, after it gets closed, I want to call DoABC function. 现在,当我关闭此子窗体时,在它关闭后,我想调用DoABC函数。

I tried using window.onbeforeunload event of child window, but it did not work for me. 我尝试使用子窗口的window.onbeforeunload事件,但对我而言不起作用。

window.onbeforeunload = function(event) {

    event = event || window.event;

    var confirmClose = 'Are you sure?';

    // For IE and Firefox prior to version 4
    if (event) {
       event.returnValue = "Are you sure";
    }

    // For Safari
    return confirmClose;

}

What could be the solution for this? 对此有什么解决方案?

When you call window.onbeforeunload, you are referring to your current window object. 调用window.onbeforeunload时,是指当前的窗口对象。 I would think this would work: 我认为这会起作用:

var newWin = window.open(....);

newWin.onbeforeonload = function () {....}

I haven't tried, but I think this will solve your problem. 我没有尝试过,但是我认为这可以解决您的问题。

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

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