【问题标题】:Internert Explorer window.open not focusing after first clickInternet Explorer 窗口。第一次单击后打开不聚焦
【发布时间】:2017-05-26 00:39:44
【问题描述】:

在 Internet Explorer 中,当我第二次单击该链接时,窗口已经打开,但没有获得焦点。它在 Chrome 和 Firefox 中运行良好。 使用以下代码:

function open_page(url, name, features) {
  var win = open(url, (name == null ? 'page' : name), (features == null ? 'menubar=1,toolbar=1,scrollbars=1,width=1024,height=768,resizable=1' : features));
  if (win == null) {
    alert('Can not open');
  } else {
    document.isUnloading = false;
    win.focus();
  }
}
<input type="image" id="pdf" name="pdf" style="padding: 2px; background-color: white; border: 1px dotted white;" title="PDF" src="/images/buttons/pdf.png" alt="PDF" onclick="open_page('http://www.w3schools.com'); return false;" onmouseover="document.getElementById('pdf').style.border='1px solid #a0a0ff'; document.getElementById('pdf').style.backgroundColor='#f0f0ff';"
onmouseout="document.getElementById('pdf').style.border='1px dotted white'; document.getElementById('pdf').style.backgroundColor='white';">

【问题讨论】:

标签: javascript html internet-explorer browser frontend


【解决方案1】:

在弹出页面添加:

<body onload="FocusWindow()">

将此函数添加到弹出窗口中引用的 JavaScript 文件中。

function FocusWindow() {
window.focus();
}

关于为什么会发生这种情况的背景信息: https://support.microsoft.com/en-us/kb/979954

基本上你不能从另一个窗口聚焦一个窗口,你只能从你想要聚焦的窗口聚焦,是的,你没看错。

所以我的解决方法是利用这个错误,让它专注于加载。

【讨论】:

  • 当我遇到这个问题时,我相信这是我让它工作的唯一方法。内联函数引用另一个文件中的 JS。这是经过多次尝试。也许你的也可以,但我知道我的可以。
  • JS需要是一个单独的文件,我知道这听起来很愚蠢,但我们这里说的是IE。
【解决方案2】:

示例 1 这应该在同一来源上工作,但可能不

var win;
function open_page(url, name, features) {
  name = name  || 'page';
  if (win && !win.closed) window.open(url,name).focus();
  else {
    win = open(url,name, (features == null ? 'menubar,toolbar,scrollbars,width=1024,height=768,resizable' : features));
    if (win == null) {
alert('Can not open.');
    } else {
      document.isUnloading = false;
      win.focus();
    }
  }
}

示例 2

这是一个更好的选择,添加

<script>window.onload=function() { window.focus() }</script>

在您打开的页面中假设没有其他 onload,如果已经有,请添加 window.focus()

如果您要打开的页面是 PDF,则使用

<script>window.onload=function() { window.focus() }</script>
<iframe height="100%" width="100%" src="yourpdfpage.pdf"></iframe>

【讨论】:

  • 我不确定在哪里添加 window.onload
  • 但是在新标签页中打开的第一页和第二次点击它在同一个水龙头中打开。最终打开了 2 页
  • 我不确定你在告诉我什么。我认为您的问题是,如果选项卡或弹出窗口打开,它不会专注于第二次点击打开页面 - 这是同一问题的两个解决方案
  • 似乎每次我点击 afetr 时都会打开新标签,我不会这样做...if (win && !win.closed) window.open(url,name).focus() ; else { win = open(url, (name == null ? 'page' : name), (features == null ? 'menubar=1,toolbar=1,scrollbars=1,width=1024,height=768,resizable= 1' : 特征));
  • 您删除了名称并使用了测试。我想看看需要什么
猜你喜欢
  • 2011-03-06
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
  • 1970-01-01
  • 2017-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多