1

感谢您花时间看我的问题。

我有两个不同的 window.open 请求,具体取决于用户是单击移动按钮还是网络摄像头按钮。

  • 单击移动按钮时, mobcamWindow将以一定(小)尺寸打开

  • 单击网络摄像头按钮时,网络摄像头窗口将以不同(大)尺寸打开

这些窗口将以正确的大小打开,但是如果在(较小的)mobcamWindow打开时单击网络摄像头按钮,我希望关闭当前的mobcamWindow并打开新的(较大的)网络摄像头窗口(以正确的大小)和反之亦然。

目前,窗口不会关闭,但 URL 将分别更改为正确的mobcamWindowwebcamWindow,但窗口将保持相同的大小。

我试过做 window.resize 没有用,window.close 也不起作用。

我的代码如下。(我有两个函数来处理每个mobcamWindowwebcamWindow,每个函数都有一个 IF 语句来检查mobcamWindowwebcamWindow是否打开,如果是则关闭它。

var webcamWindow = null; // global variable
var mobcamWindow = null; // global variable

function openStreamPopupWebcam(elem) {

  if(webcamWindow == null || webcamWindow.closed)
  /* if the pointer to the window object in memory does not exist
     or if such pointer exists but the window was closed */

  { 

    if(mobcamWindow){
      mobcamWindow.close();
    }
    /*if mobile window is open, close mobile window*/

    webcamWindow = window.open(elem.href, "window", "width=470,height=320,resizable,status").focus();
    /* then create it. The new window will be created and
       will be brought on top of any other window. */

  }

}

function openStreamPopupMobile(elem) {

  if(mobcamWindow == null || mobcamWindow.closed)
  /* if the pointer to the window object in memory does not exist
     or if such pointer exists but the window was closed */

  { 

    if(webcamWindow){
      webcamWindow.close();
    }
    /*if mobile window is open, close mobile window*/

    mobcamWindow = window.open(elem.href, "window", "width=445,height=666,resizable,status").focus();
    /* then create it. The new window will be created and
       will be brought on top of any other window. */

  }

}

如果您需要更多信息或发现难以理解我,请询问,我会尽力解释。

谢谢你。

4

2 回答 2

1

当满足这些条件时,您必须将值重新初始化为“null”

var webcamWindow = null; // global variable
var mobcamWindow = null; // global variable
function openStreamPopupWebcam(elem){
  if(webcamWindow == null || webcamWindow.closed){
    if(mobcamWindow){
      mobcamWindow.close();
      mobcamWindow = null;
    }
    webcamWindow = window.open(elem.href, "window", "width=470,height=320,resizable,status").focus();
  }
}
function openStreamPopupMobile(elem){
  if(mobcamWindow == null || mobcamWindow.closed){
    if(webcamWindow){
      webcamWindow.close();
      webcamWindow = null;
    }
    mobcamWindow = window.open(elem.href, "window", "width=445,height=666,resizable,status").focus();
  }
}

于 2015-11-17T16:31:31.017 回答
0

提姆回答

为您的窗口指定不同的名称,现在两者都被命名为 window。– 提姆

谢谢你。

还要感谢 Adarsh Mohan 指出关闭时应将值重新初始化为“null”。

于 2015-11-17T17:01:53.020 回答