2

我对 window.opener 函数(JS)有疑问:

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

测试.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 中,内容是:“从弹出窗口的第一页测试”。

错误信息:

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

3 回答 3

2

尝试parent.window.opener代替window.opener

来源: window.opener 在 Firefox 中为空

于 2014-08-18T09:22:18.727 回答
1

我通过将页面“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>
于 2014-08-18T14:57:02.243 回答
0
window.opener.document.getElementById(...) is null 

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

content in second php as defined in first php

希望这会奏效。

于 2014-08-18T13:23:47.467 回答