Javascript |检查是否已打开具有特定URL的窗口

Javascript |检查是否已打开具有特定URL的窗口,javascript,popup,window.open,Javascript,Popup,Window.open,此函数显示/ticket.html弹出窗口 我需要先检查一下窗户是否已经打开了。如果是,请取消新打开 这怎么可能呢 function popitup() { newwindow=window.open("ticket.html","_blank","toolbar=yes,scrollbars=yes, resizable=yes, top=500, left=500, width=730, height=700"); newwindow.moveTo

此函数显示/ticket.html弹出窗口

我需要先检查一下窗户是否已经打开了。如果是,请取消新打开

这怎么可能呢

function popitup() {
           newwindow=window.open("ticket.html","_blank","toolbar=yes,scrollbars=yes, resizable=yes, top=500, left=500, width=730, height=700");
           newwindow.moveTo(350,150);
       if (window.focus) 
              {
                 newwindow.focus()
              }
      }

您好,

我是javascript新手。我发现一个网站提供以下代码。但它只提供有关窗口是否提前创建的信息

function myOpenWindow(winURL, winName, winFeatures, winObj)
{
  var theWin; // this will hold our opened window 

  // first check to see if the window already exists
  if (winObj != null)
  {
    // the window has already been created, but did the user close it?
    // if so, then reopen it. Otherwise make it the active window.
    if (!winObj.closed) {
      winObj.focus();
      return winObj;
    } 
    // otherwise fall through to the code below to re-open the window
  }

  // if we get here, then the window hasn't been created yet, or it
  // was closed by the user.
  theWin = window.open(winURL, winName, winFeatures); 
  return theWin;
}

此外,访问其他选项卡的内容也可能会对用户的隐私造成攻击

打开窗口时,可以保存对该窗口的引用。
window.open
方法返回一个
windowObjectReference

通过此引用,您可以检查窗口是否关闭(
closed
property,布尔值),或者只检查窗口是否为null(
window
property,它将是
window
对象,如果关闭,则为null)

简单的例子:

// Open the pop-up and save the reference.
var windowObjRef = window.open('ticket.html');

// Verification, alternative 1. You may encapsulate this in a method.
if (windowObjRef.closed) {
    // The window is closed.
else {
    // The window is still open.
}

// Verification, alternative 2. You may encapsulate this in a method.
if (windowObjRef.window) {
    // The window is still open.
else {
    // The window is closed.
}
参考: