Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 979
Scorpyon Avatar asked Jun 04 '12 13:06

Scorpyon


People also ask

How do you check if a window is already open in JavaScript?

How to check if an opened browser window is closed or not in JavaScript? To check if an opened browser window is closed, you can use the closed property in referenced window object in JavaScript. The property returns a boolean true if the window is closed and false if the window is in the opened state.

How do I know if a popup window is closed?

Closing a popup To close a window: win. close() . To check if a window is closed: win. closed .

How do I show popups in jQuery?

Approach: A simple pop can be made by using toggle() method of jQuery which toggles between hide() and show() function of jQuery i.e. it checks the visibility of the selector used. The hide() method is run when the selector is visible and show() is run when the selector is not visible.

How do I stop popups in jQuery?

jQuery Mobile - Closing Popups You can close the popups by clicking outside the popup box or by pressing the Esc key. You can stop the closable by clicking outside the popup box using the data-dismissible = "false" attribute.


2 Answers

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(...);      
    }
}
like image 171
Fabrizio Calderan Avatar answered Sep 26 '22 01:09

Fabrizio Calderan


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.

like image 41
Omri Aharon Avatar answered Sep 25 '22 01:09

Omri Aharon