javascript可以检测窗口是否打开了另一个窗口?

时间:2013-01-30 18:16:04

标签: javascript new-window

我写了一个网页,其行为类似于一种自助服务终端:当它打开时,它会检测它是否是控制窗口。如果没有,它将加载控制窗口页面,并在新窗口中再次打开索引页面,以便它可以检测到开启指针。然后,第二个窗口包含一个链接,该链接在第三个窗口中打开任意URL。我的控制器窗口检测第二个窗口是否打开,如果任何其他窗口被聚焦,它会将该窗口带回到前面。

这个任意网站在流程的某个时刻打开另一个测验窗口(不受我的网站控制),但这个新窗口不断被推到后面,因为我的代码不断向前推进我的第二个窗口。是否有任何方式我可以检测第二个窗口是否打开了另一个窗口,如果是,请阻止我的窗口获得焦点?

以下代码在主页面上运行(index.php)

var bypass = <?=($b==1?'true':'false')?>;
if(!bypass && !window.opener && navigator.appVersion.indexOf('Mac') != -1){
    window.location = '/labs/rearWindow.php';
}

下面是我的代码,它在控制器窗口(rearWindow.php)上运行

var mainWindow = null;
var accelWindow = null;
var windowSettings = 'channelmode=yes,fullscreen=yes,height='+screen.availHeight+',width='+screen.availWidth+',left=0,top=0';

$(document).ready(function(){
    window.moveTo(0,0);
    window.resizeTo(10, 10);
    checkWindow();
});

function checkWindow(){
  if(mainWindow == null || mainWindow.closed){
    // If the main window doesn't exist or it's been closed, open new window to the index
    mainWindow = window.open('/labs/index.php', 'mainLabWindow', windowSettings);
    if(!mainWindow){
      // If the window didn't open in the satement above (usually because pop-ups are blocked), open up current window to an alternate url
      window.location = '/labs/index.php?b=1';
    }
  }else if(mainWindow != null && !mainWindow.closed && accelWindow != null && !accelWindow.closed){
    // If main window is already open and it hasn't been closed and the AR window has been opened and hasn't been closed
    accelWindow.focus();
  }else if(mainWindow != null && !mainWindow.closed && accelWindow != null && accelWindow.closed){
    // If main window has been opened and hasn't been closed and AR window has been opened but has been closed; then go back to main window
    accelWindow = null;
    mainWindow.focus();
  }else if(mainWindow != null && !mainWindow.closed){
    // If main window has been opened and has not been closed
    mainWindow.focus();
  }
  setTimeout(checkWindow, 500);
}

非常感谢您的帮助和反馈。

在第一个if中,如果我调用accelWindow.focus(),我想检查accelWindow是否有子窗口(这意味着他们打开了测验),如果是,则不要调用accelWindow.focus( )。

PS这是我的第一篇帖子/问题:)

1 个答案:

答案 0 :(得分:0)

你可以在同一个浏览器窗口内的某种框架中,在同一个应用程序中显示那些“windows”内容..你可以使用一个ligthbox。 使用基于浏览器的应用程序可以使用更多窗口,这似乎很不舒服。

相关问题