Check closed tab

Asked

Viewed 837 times

4

I found on the Internet the following code:

<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 in the system I want to do, which checks if the open tab is still open, but needs an adjustment that I do not know how to do.

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

Updating: I was able to assemble 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>
  • You want to use the setTimeout same? it will run the code once only after 2 segs, is it not the setInterval you want to use?

  • the SetTimeout in case it is to run the check until the time the user closes the popup, since I could not work with the onbeforeunload

  • Actually I didn’t see much difference rsrs

2 answers

2


You can use the function setInterval to be checking whether the popup is still open, when it is closed, you cancel the action with the 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

  • but this function setInterval would not be doing practically the same thing as the setTimer only in a different way?

  • @William Yes. But there is a setInterval will execute the code every 2 seconds in a loop until you cancel with the clearInterval, already the setTimeout will execute the code once only after the 2 seconds.

  • @William It worked out using this way?

  • Actually the setTimeout will not run only once, it will loop too! Because after two seconds it will call the function Verifica(), and if the popup is open (if (popup!=null && !popup.closed)) it will run the check again, until the popup is closed. So I did not understand the difference between the two.

  • 1

    @William You’re right, I hadn’t noticed yours 2nd code, there you call two times the setTimeout´ e verifica o status da variável popup` twice as well. Noticed some difference between your 2nd and that of the response?

  • 1

    To tell you the truth I haven’t even tested your ja I’ve seen that does the same thing hehe. But I’ll enter it into the code and see how it behaves. If it is equal mark as the answer to the question :) Otherwise I report the problem here rsrs

  • @Guilherme Managed to test the code?

  • 1

    Oops, I had forgotten to actualziar rsrs sorry! I tested yes and does the same function as mine :) Marked as best reply thank you very much.

Show 3 more comments

0

Your script is ok!

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

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

  • I didn’t understand. The moral would not be he (script) run the alert as soon as the page closes?

  • Dear William, the onbeforeunload command executes an action when a document is about to be downloaded.

  • And you know how to make him execute the command when closing the page?

  • Unfortunately I don’t know a javascript function for your purpose.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.