前台主页面给子页面赋值(回调)

1、主页面

<html>
<body>
<script type="text/javascript">
    var _callback;
    function exec(callback) {
        _callback = callback;
        document.getElementById("txtResult").value = (new Date()).toLocaleString();
    }

    function subpage() {
        if (_callback) {
            try {
                _callback("我要给子页面赋值123");
            } catch (e) {
            }
        }
    }
</script>
<input type="button" value="打开窗口" onclick="window.open('children.htm')" />
<input type="button" value="回传" onclick="subpage()" />
<input type="text" id="txtResult" />
</body>
</html>

2、子页面

<html>
<body>
<script type="text/javascript">
    function toppage() {
        opener.exec(exec1);
    }

    function exec1(msg) {
        document.getElementById("txtResult").value = msg;
    }
</script>
<input type="button" value="主页面方法" onclick="toppage()" />
<input type="text" id="txtResult" />
</body>
</html>
原文地址:https://www.cnblogs.com/honghong75042/p/4554568.html