专注于打开的窗口,而不允许另一个窗口打开不工作的IE

时间:2017-08-04 13:42:58

标签: javascript internet-explorer

此功能适用于Chrome但在IE中无效。        我想在单击乘法时间时避免打开多个窗口。        我希望它找到现有的打开窗口并使用它。

function newWindow() {
  var tab;
  tab = window.open('http://google.com', 'MyTab', 'height=800px,width=1500px,resizable=yes');
  tab.focus();
}
<body>
  force to open tab in same window
  <a href="#" onclick="newWindow();">New Window</a>
</body>

1 个答案:

答案 0 :(得分:0)

看起来,IE会抛出一个&#34; Access denied&#34;尝试重新打开tab时出错(由于跨源内容,但为什么,目前尚不清楚)。这会停止脚本,并且永远不会执行tab.focus()

要解决此问题,请将tab的定义移至外部作用域,然后将window.open(...)换行到try..catch,如下所示:

var tab;
function newWindow() {
    try {
    tab = window.open('http://google.com', 'MyTab', 'height=800px,width=1500px,resizable=yes');
  } catch (err) {;}
  if (tab) {
     tab.focus();
  }
}

通过这种方式,您可以存储原始tab,而tab.focus()会将其显示在屏幕上的主风面前。

相关问题