简体   繁体   English

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

[英]javascript alert on parent page when child popup closes

I am using javascript window.open method to open lets say http://www.google.com [its always going to be some external url] 我正在使用javascript window.open方法打开,说http://www.google.com [它始终是一些外部网址]

I have stored the reference of the window object in a variable, the problem is that variable never gets null on the parent page and I am not able to alert the user that the pop up has been closed. 我已经将窗口对象的引用存储在一个变量中,问题是该变量在父页面上永远不会为空,并且我无法提醒用户弹出窗口已关闭。

Here's the code 这是代码

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

I can not put any javascript code on the pop up/child window. 我不能在弹出/子窗口中放置任何JavaScript代码。

Hope someone can help me on this 希望有人可以帮助我

The window variable doesn't get nulled out when it's closed, however its .closed property is true , so just change your check to be for that property, like this: window变量在关闭时不会消失,但是它的.closed属性true ,因此只需将检查更改为该属性即可,如下所示:

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

Also note the setTimeout() change, pass a function in whenever possible (almost always ) rather than a string, you'll have a lot less problems with scoping. 还要注意setTimeout()更改,在任何可能的情况下(几乎总是 )而不是字符串中传递一个函数,您将在范围界定方面遇到很多麻烦。

//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()"

hope this gives you some direction.. 希望这能给您一些指导。

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

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