window.opener.focus()在IE中失败,并在FireFox中工作

时间:2009-05-18 07:05:56

标签: javascript html window.opener

我有一个问题,当我打电话时,IE无法启动开启窗口 opener.focus()方法

window.opener.focus(); // 之后,子窗口停留在前面。


html1.htm 档案:

<script type="text/javascript" language="JavaScript"><!--
function toCompare() {
    wCompare = window.open("html2.htm", "wCompare", "width=800,height=600,resizable=yes,directories=no,status=no,toolbar=no,menubar=0,location=no,scrollbars=yes");
    wCompare.focus();
};
//--></script>
</head>
<body>

<a href="javascript://" onClick="toCompare();">open child window</a>

</body>

html2.htm

<script type="text/javascript" language="JavaScript"><!--
      function show_Parent(url) {
          window.opener.location.href = url;
          window.opener.focus(); // After that, child window stay in front.
       }
    //--></script>
</head>
<body>

<a onclick="return show_Parent('html3.htm');">go back to parent window</a>

</body>

2 个答案:

答案 0 :(得分:2)

这对我来说很好,但我看到过类似的行为。尝试在父页面中创建一个功能,以抓住它自己的焦点并更改URL

<强> html1.htm

function focusAndGo(url) {
   window.focus();
   // EDIT: changed document.location.href= to window.location.href=
   // Reference:
   // https://developer.mozilla.org/En/Document.location
   // document.location was originally a read-only property,
   // although Gecko browsers allow you to assign to it as well.
   // For cross-browser safety, use window.location instead.
   window.location.href=url;
   }

并从 html2.htm

调用此方法
window.opener.focusAndGo(url);

答案 1 :(得分:0)

首先尝试模糊当前窗口,这可能会有所帮助。

window.blur();
window.opener.focus();