【问题标题】:IE7 window.open when .focus return nullIE7 window.open 当 .focus 返回 null
【发布时间】:2011-04-24 19:29:05
【问题描述】:

我正在尝试做这样的事情

win = null;
win = window.open('/url/to/link','tab');
win.focus();

但在 IE7 中,它会在 win.focus(); 行返回我win 为 null 的错误。

我该如何解决?

提前致谢!

【问题讨论】:

  • 窗口打开了吗?无论如何,这不适用于 Firefox、Chrome 和 Safari——它们通常不允许一个窗口强制另一个窗口获得焦点。
  • 在 Firefox 中也出现错误

标签: javascript internet-explorer-7


【解决方案1】:

您可以尝试添加一点延迟以确保窗口处于打开状态

//win = null;  <--useless
win = window.open('/url/to/link','tab');
if(win)window.focus();
else{
    var timer = window.setTimeout( function(){ if(win)win.focus(); }, 100 );
}

当今时代,大多数人避免弹出窗口并使用模态层。

【讨论】:

  • This day in age, most people avoid pop up windows and use modal layers. 如果你想打开一个外部链接但由于某种原因不能使用锚标签怎么办?
【解决方案2】:

块引用

返回对新窗口对象的引用。使用此引用访问新窗口上的属性和方法。

Windows Vista 上的 Internet Explorer 7:从应用程序(Internet Explorer 进程除外)打开新窗口可能会导致返回空值。出现此限制是因为 Internet Explorer 默认在保护模式下运行。当访问跨越进程边界时,保护模式的一个方面会阻止应用程序对 Internet Explorer 进行特权访问。使用此方法打开一个新窗口会生成一个新进程。有关保护模式的详细信息,请参阅了解和使用保护模式 Internet Explorer。这通常发生在承载 WebBrowser 控件的应用程序中。> Window.Open method documentation

【讨论】:

  • 这是什么Blockquote &lt; Return Value?请您提供更多信息。
  • 第一行应该是“返回值”。它只是对我在文档中链接到的页面中间的段落的引用,这解释了为什么“返回值”可能为空。
【解决方案3】:

当你启动一个弹出窗口时,给它一个变量名:

myWin = window.open(etc)

//in the child window, call window.opener.myFocusFunction()
//in the parent window, use this...

function myFocusFunction(){
   myWin.focus();
   //myWin.blur();
   //uncomment as needed!
}

玩一玩,对我有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    相关资源
    最近更新 更多