我正在使用javascript window.open方法打开,说http://www.google.com [它总是会是一些外部URL]

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

这是代码

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

            如果(counterFB == 0){
                winFB = window.open(“ http://www.google.com”);
                counterFB = 1;
            }
            如果(counterFB> 0){
                alert(winFB);
                如果(winFB == null){
                    counterFB = 0;
                    clearTimeout(timerFB);
                    alert(“关闭窗口”);
                }
            }
          timerFB = setTimeout(“ openFB()”,1000);
        }

    


我不能在弹出/子窗口中放置任何JavaScript代码。

希望有人可以帮助我

最佳答案

window变量在关闭时不会消失,但是它的.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()的更改,在任何可能的情况下(几乎总是)在函数中传递一个函数,而不是在一个字符串中传递函数,这将减少范围界定的麻烦。

10-06 07:48