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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10882112/
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-08-24 03:24:09  来源:igfitidea点击:

Check if a popup window is already open before opening it using Jquery

javascriptjqueryhtml

提问by Scorpyon

I want to check if a popup window is already open , before I open the popup window. How do I get it done using Jquery?

我想在打开弹出窗口之前检查弹出窗口是否已经打开。我如何使用 Jquery 完成它?

Below is my code to open a new popup window :

下面是我打开一个新弹出窗口的代码:

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

Now before I call this, I want to be sure that this popup window is not already open.

现在在我调用它之前,我想确保这个弹出窗口还没有打开。

回答by Omri Aharon

This is a little trick I use, perhaps you could use it:

这是我使用的一个小技巧,也许你可以使用它:

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

Hope it helps.

希望能帮助到你。

回答by Fabrizio Calderan

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

回答by Fabiano Couto

Here is my suggestion:

这是我的建议:

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

回答by Shehzad Bilal

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

回答by kleinohad

try this (you will know if the open window was called):

试试这个(你会知道是否调用了打开的窗口):

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