Robin
Robin

Reputation: 709

jQuery: How to detect if popup window is closed?

I want to detect if the user pressed the browser's close button on a popup window in my application.

By searching around I found this solution:

      $(window).bind('beforeunload', function(event) {
      $.ajax({url:"acquisition_cleanup.php", async:false});
      var dataStr = 'id={id}';
      $.ajax({
          type: "POST",
          url: "acquisition_cleanup.php",
          data: dataStr,
          success: function() {
            window.close();
          }
      });
  });

It works well on my Ubuntu machine with Firefox 31. But unfortunately it does not work on windows machine with the same Firefox-Browser. How can I fix this issue?

// EDIT

This is the function I use to open the window:

function popup (url) {
win = window.open(url, "Fenster",    "width=1200,height=600,resizable=yes,menubar=no,toolbar=no,status=no,location=no,directories    =no");
win.focus();
return false;
}

EDIT 2 //

I added this to the global.js

function checkWindowClosed(url, progress) {
var yourwindowname; // add in Global

if(yourwindowname==undefined){
    var param= "toolbar=no,scrollbars=1";
    yourwindowname=window.open(url, "Fenster", param);
}else{
    yourwindowname.focus();
}

var timer = setInterval(function() {
    if(yourwindowname.closed) {
        clearInterval(timer);
        progress();
        alert("Popup Closed");
    }else{
        yourwindowname.close();
    }
}, 1000);
}

And in the HTML-File I used the function like this, but it still does not work:

        checkWindowClosed("aquisition.php", function() {
        $.ajax({url:"acquisition_cleanup.php", async:true});
        var dataStr = 'id={id}';
        $.ajax({
            type: "POST",
            url: "acquisition_cleanup.php",
            data: dataStr,
            success: function () {
                window.close();
            }
        });
    });

Upvotes: 0

Views: 12083

Answers (1)

Krish R
Krish R

Reputation: 22711

Can you try this, You can use the below code to check the browser popup closed status.

var yourwindowname; // add in Global    

/*Added this for to open a browser popup window */ 
if(yourwindowname==undefined){
    var param= "toolbar=no,scrollbars=1";
    yourwindowname=window.open("your url here", 'yourwindowname', param);
}else{
    yourwindowname.focus();     
}

/* Added this to Check the popup closed status - In your case on ajax success event  */
var timer = setInterval(function() {   
    if(yourwindowname.closed) {                             
        clearInterval(timer);  
        //do your process here
        alert("Popup Closed");
    }else{
       yourwindowname.close();
    }  
}, 1000); 

Updated code for your working project: Use the below code instead of your current javascript functions.

function popup (url) {          
    if(win==undefined){
        win = window.open(url, "Fenster",    "width=1200,height=600,resizable=yes,menubar=no,toolbar=no,status=no,location=no,directories=no"); 
    }else{
        win.focus();
    }       
    return false;
}

function checkWindowClosed(){
    var timer = setInterval(function() {
        if(win.closed) {
            clearInterval(timer);
            progress();
            alert("Popup Closed");
        }else{
            win.close();
        }
    }, 1000);
}

  $.ajax({
        type: "POST",
        url: "acquisition_cleanup.php",
        data: dataStr,
        success: function () {
            checkWindowClosed();
        }
    });

Upvotes: 1

Related Questions