【问题标题】:window.opener doesn't work in firefox and chrome but in IEwindow.opener 在 Firefox 和 chrome 中不起作用,但在 IE 中不起作用
【发布时间】:2014-08-18 08:52:35
【问题描述】:

window.opener 函数 (JS) 有问题:

我创建了 3 个简单的页面来模拟我的问题:

test.php:

<html>
    <head>
    </head>
    <body>
        <input type="button" onclick="window.open('first_page_in_popup.php', 'Popup', 'width=470,height=500');">    
        <input type="text" id="content" name="content" >
    </body>
</html>

first_page_in_popup.php:

<html>
    <head>
        <script type="text/javascript" >
            window.opener.document.getElementById('content').value= "Test from first page in popup";
            window.open('second_page_in_popup.php', 'Popup');
        </script>
    </head>
    <body>
    </body>
</html>

second_page_in_popup.php:

<html>
    <head>
        <script type="text/javascript" >
            window.opener.document.getElementById('content').value= "Test from second page in popup";
        </script>
    </head>
    <body>
    </body>
</html>

结果: 在 IE 中,输入字段的内容为“Test from second page in popup”。 在 Chrome 和 Firefox 中,内容为:“Test from first page in popup”。

错误信息:

window.opener.document.getElementById(...) is null

【问题讨论】:

  • 这可能是一个安全限制。我经历过 Chrome 拒绝让弹出窗口获取/设置开启器的值。您是否尝试在从弹出窗口调用的主窗口中创建一个函数?
  • 在 Firefox 中效果很好,我可以从弹出窗口中执行此操作并替换内容:window.opener.document.getElementById('maincontent').innerHTML = "test";你能创建一个小提琴来重现你的问题吗?
  • 如果我将第二个弹出窗口重命名为“Popup2”并将第二页的代码更改为:window.opener.opener.document.getElementById('content').value="从第二页开始测试弹出窗口”;有用。但现在我必须打开弹出窗口......我只想要一个窗口。

标签: javascript google-chrome firefox window.open window.opener


【解决方案1】:

尝试parent.window.opener 而不是window.opener

来源: window.opener is null in firefox

【讨论】:

    【解决方案2】:

    我通过将页面“first_page_in_popup.php”的代码更改为:

    <html>
        <head>
            <script type="text/javascript" >
                window.opener.document.getElementById('content').value= "Test from first page in popup";
                window.location.href = 'second_page_in_popup.php';
    
            </script>
        </head>
        <body>
            <p> First Page </p> 
            <input type="text" id="first" name="first">
        </body>
    </html>
    

    【讨论】:

      【解决方案3】:
      window.opener.document.getElementById(...) is null 
      

      这意味着 window.opener.document.getElementById 中的内容为 null 或未定义 请尝试定义

      content in second php as defined in first php
      

      希望这会奏效。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-03
        • 1970-01-01
        • 1970-01-01
        • 2011-05-03
        • 1970-01-01
        • 2014-08-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多