感谢您花时间查看我的问题。
我有两个不同的 window.open 请求,具体取决于用户是否单击移动按钮或网络摄像头按钮。
单击移动按钮时,mobcamWindow 将以特定(小)尺寸打开
单击网络摄像头按钮时,网络摄像头窗口将以不同(大)尺寸打开
这些窗口可以正常打开且大小正确,但是,如果在(较小的)mobcamWindow 打开时单击网络摄像头按钮,我希望当前的 mobcamWindow关闭并打开新的(更大的)webcamWindow(以其正确的大小),反之亦然。
目前,窗口不会关闭,但 URL 将分别更改为正确的 mobcamWindow 和 webcamWindow,但窗口将保持相同的大小。
我尝试过window.resize,但没有成功,window.close也不起作用。
我的代码如下。 (我有两个函数来处理每个 mobcamWindow 和 webcamWindow,每个函数中都有一个 IF 语句来检查 mobcamWindow 或 webcamWindow strong> 已打开,如果已打开则将其关闭。
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();
}
}
关于javascript - 当请求另一个 window.open 时 window.close 当前窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33761078/