当子弹出窗口关闭时,父页面上的javascript警报

时间:2010-11-23 10:07:47

标签: javascript window.open

我正在使用javascript window.open方法打开让我们说http://www.google.com [它总是会成为一些外部网址]

我已将窗口对象的引用存储在变量中,问题是变量永远不会在父页面上变为空,我无法提醒用户弹出窗口已被关闭。

这是代码

 
        var winFB;
        var winTWt;
        var counterFB = 0;
        var counterTWT = 0;
        var timerFB;
        function openFB() {

            if (counterFB == 0) {
                winFB = window.open("http://www.google.com");
                counterFB = 1;
            }
            if (counterFB > 0) {
                alert(winFB);
                if (winFB == null) {
                    counterFB = 0;
                    clearTimeout(timerFB);
                    alert("Window Closed");
                }
            }
          timerFB= setTimeout("openFB()", 1000);
        }

    

我无法在弹出/子窗口中放置任何javascript代码。

希望有人可以帮助我

2 个答案:

答案 0 :(得分:4)

窗口变量在关闭时不会被取消,但其.closed propertytrue,所以只需将检查更改为该属性,如下所示:

var winFB;
var winTWt;
var counterFB = 0;
var counterTWT = 0;
var timerFB;
function openFB() {
    if (counterFB == 0) {
        winFB = window.open("http://www.google.com");
        counterFB = 1;
    }
    if (counterFB > 0) {
        if (winFB.closed) {
            counterFB = 0;
            clearTimeout(timerFB);
            alert("Window Closed");
        }
    }
    timerFB = setTimeout(openFB, 1000);
}

另请注意setTimeout()更改,尽可能传递函数(几乎始终)而不是字符串,您可以减少范围问题。

答案 1 :(得分:1)

//in the parent
winFB = window.open("http://www.google.com");

//in the child window you can access
var _parent = window.opener.document;

//also use the onunload event in the child window
onunload="doSomething()"

希望这会给你一些指导..