javascript jQuery:如何检测弹出窗口是否关闭?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24994791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:42:29  来源:igfitidea点击:

jQuery: How to detect if popup window is closed?

javascriptjqueryjavascript-eventswindow

提问by Robin

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 unfortunatly it does not work on windows machine with the same Firefox-Browser. How can I fix this issue? Thanks for your help!

它在我的带有 Firefox 31 的 Ubuntu 机器上运行良好。但不幸的是,它不能在具有相同 Firefox 浏览器的 Windows 机器上运行。我该如何解决这个问题?谢谢你的帮助!

// 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 //

编辑 2 //

I added this to the global.js

我将此添加到 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:

在 HTML 文件中,我使用了这样的函数,但它仍然不起作用:

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

回答by Krish R

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.

为您的工作项目更新代码:使用以下代码代替您当前的 javascript 函数。

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