window.opener没有在chrome和Opera上工作

时间:2014-04-12 13:20:27

标签: javascript window.opener

尝试了this link的接受答案 使用top.window.opener来访问parent.html。没有运气。下面是我的代码

parentWindow.html               

<p>Click the button to write some text to the source (parent) window.</p>

<button onclick="openWin()">Open "myWindow"</button>

<script>
function test1()
{
    alert("test1");
}
function openWin()
{
var myWindow = window.open("childWindow.html");

}
</script>

</body>
</html>

childWindow.html

<HTML>
<HEAD>
  <title>Child</title>

<SCRIPT type="text/javascript" LANGUAGE="javascript">

function Initialize()
{
     try{
        if(top.window.opener != null && !top.window.opener.closed)
        {
          top.window.opener.test1();
        }

    }catch(e){ alert(e.description);}  


}
</script>
</HEAD>
<BODY onload="Initialize()">

</BODY>
</HTML>

在遵循this link的摘录后,在服务器上尝试了相同的代码。 Dint help。

2 个答案:

答案 0 :(得分:2)

根据标准,如果省略第二个参数,则默认为_blank,但所有浏览器可能都不遵循该标准。指定目标,以便您知道它确实在新窗口中打开并且不替换当前窗口:

window.open("childWindow.html", "_blank");

答案 1 :(得分:0)

我想你想做的是parent.window.opener

    if(parent.window.opener != null && ! parent.window.opener)
    {
      parent.window.opener.test1();
    }

希望这有帮助。