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

前端 未结 5 1316
广开言路
广开言路 2021-02-08 11:44

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 :

5条回答
  •  既然无缘
    2021-02-08 12:14

    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.

提交回复
热议问题