jQuery:如何检测弹出窗口是否关闭? [英] 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();
}
});
});
在使用Firefox 31的Ubuntu机器上运行良好但不幸的是它在Windows机器上运行不正常相同的Firefox浏览器。我该如何解决这个问题?感谢您的帮助!
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!
//编辑
这是我用来打开窗口的功能:
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;
}
编辑2 //
我将此添加到global.js
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);
}
在HTML文件中我使用了这样的函数,但它仍然不起作用:
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();
}
});
});
推荐答案
你试试这个吗,你可以使用下面的代码检查浏览器弹出窗口关闭状态。
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);
更新了您的工作项目代码:使用以下代码代替您的代码当前的javascript函数。
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();
}
});
这篇关于jQuery:如何检测弹出窗口是否关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!