Scorpyon
Scorpyon

Reputation: 236

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

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?

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.

Upvotes: 10

Views: 41826

Answers (5)

Omri Aharon
Omri Aharon

Reputation: 17064

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.

Upvotes: 10

Fabiano Couto
Fabiano Couto

Reputation: 31

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

Upvotes: 0

Shehzad Bilal
Shehzad Bilal

Reputation: 2523

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

Upvotes: 0

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123367

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

Upvotes: 6

kleinohad
kleinohad

Reputation: 5912

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

Upvotes: -2

Related Questions