【问题标题】:jQuery: How to detect if popup window is closed?jQuery:如何检测弹出窗口是否关闭?
【发布时间】:2014-07-28 12:00:38
【问题描述】:

我想检测用户是否在我的应用程序的弹出窗口中按下了浏览器的关闭按钮。

通过搜索我找到了这个解决方案:

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

它在我的带有 Firefox 31 的 Ubuntu 机器上运行良好。但不幸的是,它不适用于带有相同 Firefox 浏览器的 Windows 机器。我该如何解决这个问题?

// 编辑

这是我用来打开窗口的函数:

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

编辑 2 //

我将此添加到 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);
}

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

【问题讨论】:

  • 您使用的是哪种弹出窗口?我可以立即为您提供引导弹出窗口的答案,但我不知道这是否是您想要的
  • @user3127242 我对此表示怀疑,因为问题或标签中没有任何提及。
  • 我在原帖中添加了代码。
  • PS:我需要检查弹出窗口是否在弹出窗口中关闭,因为索引没有必要的数据...

标签: javascript jquery jquery-events popupwindow


【解决方案1】:

你可以试试这个,你可以使用下面的代码来检查浏览器弹出关闭状态。

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

为您的工作项目更新了代码:使用以下代码代替您当前的 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();
        }
    });

【讨论】:

    猜你喜欢
    • 2011-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    相关资源
    最近更新 更多