代码之家  ›  专栏  ›  技术社区  ›  Greg Reynolds

仅当窗口不存在时才打开。

  •  21
  • Greg Reynolds  · 技术社区  · 16 年前

    我有一个应用程序可以在单击链接时打开一个新窗口。这产生了一个包含Java applet的页面。我所面临的问题是单击同一个链接重新加载页面,该页面重置Java应用程序。有什么办法可以把这个困住吗?可接受的两种解决方案是:

    1. 允许从单击处理程序打开多个窗口
    2. 如果窗口已打开,则忽略后续请求

    为成为一个javascript新手道歉-这不是我的主要事情。

    附加到处理程序的代码是

    function launchApplication(l_url, l_windowName)
    {
      var l_width = screen.availWidth;
      var l_height = screen.availHeight;
    
      var l_params = 'status=1' +
                     ',resizable=1' +
                     ',scrollbars=1' +
                     ',width=' + l_width +
                     ',height=' + l_height +
                     ',left=0' +
                     ',top=0';
    
      winRef = window.open(l_url, l_windowName, l_params);
      winRef.moveTo(0,0);
      winRef.resizeTo(l_width, l_height);
    }
    

    编辑:

    感谢您的回复-我稍微修改了一些建议,这样我可以通过函数打开多个URL。

    编辑2: 此代码的另一个版本位于 Check for a URL open on another window

    var g_urlarray = [];
    
    Array.prototype.has = function(value) {
        var i;
        for (var i in this) {
            if (i === value) {
                return true;
            }
        }
        return false;
    };
    
    
    function launchApplication(l_url, l_windowName)
    {
      var l_width = screen.availWidth;
      var l_height = screen.availHeight;
      var winRef;
    
      var l_params = 'status=1' +
                     ',resizable=1' +
                     ',scrollbars=1' +
                     ',width=' + l_width +
                     ',height=' + l_height +
                     ',left=0' +
             ',top=0';
      if (g_urlarray.has(l_url)) {
        winRef = g_urlarray[l_url];
      }
      alert(winRef);
      if (winRef == null || winRef.closed) {
          winRef = window.open(l_url, l_windowName, l_params);
          winRef.moveTo(0,0);
          winRef.resizeTo(l_width, l_height);
          g_urlarray[l_url] = winRef;
      }
    }
    
    6 回复  |  直到 9 年前
        1
  •  19
  •   Peter Bailey    10 年前

    我会这样做-基本上存储函数本身上所有引用的打开的窗口。当函数启动时,检查窗口是否不存在或已经关闭,启动弹出窗口。否则,请关注该请求的现有弹出窗口。

    function launchApplication(l_url, l_windowName)
    {
      if ( typeof launchApplication.winRefs == 'undefined' )
      {
        launchApplication.winRefs = {};
      }
      if ( typeof launchApplication.winRefs[l_windowName] == 'undefined' || launchApplication.winRefs[l_windowName].closed )
      {
        var l_width = screen.availWidth;
        var l_height = screen.availHeight;
    
        var l_params = 'status=1' +
                       ',resizable=1' +
                       ',scrollbars=1' +
                       ',width=' + l_width +
                       ',height=' + l_height +
                       ',left=0' +
                       ',top=0';
    
        launchApplication.winRefs[l_windowName] = window.open(l_url, l_windowName, l_params);
        launchApplication.winRefs[l_windowName].moveTo(0,0);
        launchApplication.winRefs[l_windowName].resizeTo(l_width, l_height);
      } else {
        launchApplication.winRefs[l_windowName].focus()
      }
    }
    
        2
  •  20
  •   scunliffe    16 年前

    您需要执行两个测试…1检查弹出窗口是否定义,2检查是否关闭。

    if(typeof(winRef) == 'undefined' || winRef.closed){
      //create new
      winRef = window.open(....);
    } else {
      //it exists, load new content (if necs.)
      winRef.location.href = 'your new url';
      //give it focus (in case it got burried)
      winRef.focus();
    }
    
        3
  •  5
  •   M4N    16 年前

    您可以在打开新窗口的页面中使用类似的内容:

    var newWindow = null;
    
    function launchApplication()
    {
      // 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(...);
    }
    
        4
  •  3
  •   Ivan Drinchev    13 年前

    工作代码

    var newwindow = null;
    function popitup(url) {
        if ((newwindow == null) || (newwindow.closed)) {
            newwindow=window.open(url,'Buy','width=950,height=650,scrollbars=yes,resizable=yes');
            newwindow.focus();
        } else {
            newwindow.location.href = url;
            newwindow.focus();    
        }
    }  
    
        5
  •  2
  •   Phaedrus    16 年前

    您可以这样检查:

    if(!winref || winref.closed)
    {
    }
    
        6
  •  0
  •   chrism    13 年前

    尝试检查:

    如果(!)winref winref.closed!WinRef.文档){ }

    推荐文章