如何在javascript中关闭子窗口调用父窗口函数?

时间:2014-04-01 02:26:19

标签: javascript jquery html dom

//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"); 
}

现在当我关闭这个子表单时,在它关闭之后,我想调用DoABC函数。

我尝试使用子窗口的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;

}

这可能是什么解决方案?

1 个答案:

答案 0 :(得分:1)

当你调用window.onbeforeunload时,你指的是你当前的窗口对象。我认为这会奏效:

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

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

我没试过,但我认为这会解决你的问题。