感谢您抽出宝贵时间来查看我的问题。

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


单击移动按钮时,mobcamWindow将以特定(小)大小打开
单击摄像头按钮时,摄像头窗口将以其他(大)尺寸打开


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

当前,窗口不会关闭,但是URL将分别更改为正确的mobcamWindow和webcamWindow,但是窗口将保持相同大小。

我试图做不起作用的window.resize,并且window.close也不起作用。

我的代码如下。 (我有两个函数来处理每个mobcamWindow和webcamWindow,每个函数中都有一个IF语句,以检查mobcamWindow或webcamWindow是否打开,如果关闭,则将其关闭。

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. */

  }

}


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

谢谢。

最佳答案

满足这些条件时,您必须将值重新初始化为“ 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();
  }
}

09-25 21:31