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

时间:2014-11-25 11:56:25

标签: 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(350,150);
       if (window.focus) 
              {
                 newwindow.focus()
              }
      }

此致

2 个答案:

答案 0 :(得分:1)

我是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;
}

此外,访问其他标签的内容也可能会对用户的隐私造成攻击。

答案 1 :(得分:1)

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

使用此引用,您可以检查窗口是否已关闭(closed属性,布尔值),或者只是窗口为空(window属性,这将是Window object或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.
}

参考:https://developer.mozilla.org/en-US/docs/Web/API/Window