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

Sco*_*yon 9 html javascript jquery

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

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

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

在我调用之前,我想确保此弹出窗口尚未打开.

Omr*_*ron 9

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

    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();
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

希望能帮助到你.

  • 是的,它在 2014 年对某人有帮助!:) (2认同)

fca*_*ran 5

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(...);      
    }
}
Run Code Online (Sandbox Code Playgroud)


She*_*lal 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(...);
}
Run Code Online (Sandbox Code Playgroud)