使用Jquery在打开弹出窗口之前检查它是否已经打开

10

我想要在打开弹出窗口之前检查该弹出窗口是否已经打开。如何使用Jquery实现此功能?

以下是我的代码,用于打开新的弹出窗口:

window.open("mystopchat.php?stat=1&session="+data['myid1']['session_id'][i],"win1","width=500,height=500"); 

在调用这个函数之前,我想确保这个弹出窗口没有已经打开了。


请查看此帖子,或许可以帮到您。 - Anthony Atkinson
取一个变量名为ispopup,将其设为“false”。在打开窗口后将其设为true,例如:if(ispopup != true){window.open()} else {//某些东西已经打开;} - Tamkeen
5个回答

10
这是我使用的一种小技巧,也许你也可以用它:
var winRef; //This holds the reference to your page, to see later it is open or not

function openWindow() {  
    var url = //Your URL;
    if (typeof (winRef) == 'undefined' || winRef.closed) {
        //create new, since none is open
        winRef = window.open(url, "_blank");
    }
    else {
        try {
            winRef.document; //if this throws an exception then we have no access to the child window - probably domain change so we open a new window
        }
        catch (e) {
            winRef = window.open(url, "_blank");
        }

        //IE doesn't allow focus, so I close it and open a new one
        if (navigator.appName == 'Microsoft Internet Explorer') {
            winRef.close();
            winRef = window.open(url, "_blank");
        }
        else {
            //give it focus for a better user experience
            winRef.focus();
        }
    }
}

希望这能有所帮助。

2
是的,它在2014年对某人有所帮助! :) - Chankey Pathak
@ChankeyPathak 很高兴能帮忙 :) - Omri Aharon

6
var popup;
function openPopupOneAtATime() {
    if (popup && !popup.closed) {
       popup.focus();
       /* or do something else, e.g. close the popup or alert a warning */
    }
    else {
       popup = window.open(...);      
    }
}

0
var newWindow = null;

function openwindow()
{
  // open the new window only if newWindow is null (not opened yet)
  // or if it was closed
  if ((newWindow == null) || (newWindow.closed))
    newWindow = window.open(...);
}

0

这是我的建议:

function authorize(callback) {

if(!document.authorize) {
    console.log('Opening authorization window...');
    document.authorize = window.open('popup.html','tw','width=300,height=200');
}

if(document.authorize.closed) {
    console.log('Authorization window was closed...');
    setTimeout(callback,0); 
} else {
    setTimeout(function(){
        console.log('Authorization window still open...');
        authorize(callback);
    },1000);    
}

return false;
}


function test() {
    authorize(function(){
      alert('teste');
    });
}

-1

试试这个(如果打开窗口就会知道):

var isOpen = "false";
function OpenPopup()
{
   if(isOpen == "false")
   {
         isOpen = "true"; 
         window.open("mystopchat.php?stat=1&session="+data['myid1']['session_id']  [i],"win1","width=500,height=500");
    }
} 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接