基本html页面和弹出窗口之间的数据交换

Data exchange between base html page and popup window

本文关键字:之间 窗口 数据 交换 html 基本      更新时间:2023-09-26

我有这个index.php文件;

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Proj</title>
        <script type="text/javascript">
            function openWindow() {
                window.open("popup.php", "popup_id", "scrollbars=no,resizable,width=200,,left=300,top=300,height=200");
            }
        </script>
    </head>
    <body>
        <form name="form1" action="">
            <input name="initialdata" type="text" value="initial data" />
            <input type="button" value="Send" onClick="openWindow()" />
        </form>
    </body>
</html>

和这个popup.php文件

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>popup</title>
        <script type="text/javascript">
            function putData() {
                //How to copy from that file -> document.formname.popupdata.value to 
                //index.php -> document.form1.initialdata.value???
                window.close();
            }
            function loadData() {
                //how to get the value "initial data"
                //from index.php -> document.form1.initialdata.value????????????
            }
        </script>
    </head>
    <body onLoad="loadData();">
        <form name="formname" action="">
            <input name="popupdata" type="text" value="" />
            <input type="button" value="Send" onClick="putData()" />
        </form>
    </body>
</html>

如何从index.php->document.form1.initialdata.value获取值"initial data",以及如何从popup.php->document.formname.popupdata.value复制到index.php->document.form1.initialdata.value??

使用

window.opener.document.getElementsByName("initialdata")[0].value;

不过,这可能不适用于所有浏览器。有关详细信息,请参阅此问题:

window.opener替代