Check closed flap

4

I found the following code on the internet:

<script type="text/javascript">
var popup = null;

function Abrir() {
    if (popup!=null && popup.closed)
    alert("A Janela foi fechada.n Abrindo novamente.");

    if (popup!=null && !popup.closed){
    }
    else {
        popup = window.open("http://www.google.com","Jan","height=350,width=200");popup.focus();
    }
    setTimeout("Abrir()", 2000); //javascript trabalha com milesimos
}
</script>

It fits well into the system I want to do, which checks to see if the open flap is still open, but needs an adjustment that I do not know how to do.

This code is in loop , when it gives the message that the window has been closed, it opens it again. Would he just send the message? And to order only once not to be sent all the time? That is, make the timer run only until it detects that the window has been closed, so it issues the alert and stops running the function again.

Update : I was able to mount the following code, what do you think?

<script type="text/javascript">
var popup = null;

function Abrir() {
    popup = window.open("http://www.google.com","Jan","height=800,width=600");popup.focus();
    setTimeout("Verifica()", 2000);
}
function Verifica() {
    if (popup!=null && popup.closed){
    $("#aguardetop").hide("slow");
    $("#sucessotop").fadeIn();
    Adiciona();
    return;
    }
    if (popup!=null && !popup.closed){
    }
    setTimeout("Verifica()", 2000);
}
</script>
    
asked by anonymous 12.05.2015 / 00:10

2 answers

2

You can use the setInterval function to be checking if the popup is still open, when it is closed, you cancel the action with function clearInterval .

function aoFecharJanela(){
  alert("A janela foi fechada pelo usuário");
}
function abrirPopup(url, windowName, opts, callback) {
    var popup = window.open(url, windowName, opts);
    var intervalo = setInterval(function() {
        try {
            if (popup == null || popup.closed) {
                window.clearInterval(intervalo);
                callback(popup);
            }
        }
        catch (e) { }
    }, 2000);
    return popup;
}

abrirPopup("http://www.google.com","Jan","height=350,width=200", aoFecharJanela);

Exemplo

    
14.05.2015 / 00:39
0

Your script is ok!

onbeforeunload will only be fired the moment the open window is about to load the inner page. If you click the close button at this time, alert will be triggered normally.

Chrome and Firefox has this behavior. IE , deny action and nothing happens.

    
12.05.2015 / 03:37