Javascript 如何将数据从弹出窗口传递到父窗口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12129121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:10:36  来源:igfitidea点击:

How to pass data to parent window from popup window?

javascriptjquerypopup

提问by Click Upvote

How can I pass some data or call a function on the parent window from a popup window?

如何从弹出窗口传递一些数据或调用父窗口上的函数?

The user will click a link which will open a popup on the same website, once he is finished with the popup, I want it to send the new data back to the parent window, or call a function on the parent window.

用户将单击将在同一网站上打开弹出窗口的链接,一旦他完成弹出窗口,我希望它将新数据发送回父窗口,或调用父窗口上的函数。

回答by Hoff

The window.openerobject is what you're looking for, used it from within your popup like so to call the a function of the parent window:

window.opener对象是你在找什么,从用它的弹出内像这样调用父窗口的功能:

window.opener.yourFunc() 

回答by Ryan

Here is a fun and easy demothat is heavily inspired by this answer to a similar question(but modified for my own purposes to help investigate the most difficult bug of my career).

这是一个有趣且简单的演示,其灵感来自对类似问题的回答(但出于我自己的目的进行了修改,以帮助调查我职业生涯中最困难的错误)。

Create 2 files (in the same directory) as follows:

创建2个文件(在同一目录下)如下:

parent.html

父.html

<button type="button" onclick="popup('popup.html', '', 800, 200);">Add My Card</button>
=&gt;
<span id="retrievedData">No data yet.</span>    
<script>
    function popup(url, title, width, height) {
        var left = (screen.width / 2) - (width / 2);
        var top = (screen.height / 2) - (height / 2);
        var options = '';    
        options += ',width=' + width;
        options += ',height=' + height;
        options += ',top=' + top;
        options += ',left=' + left;    
        return window.open(url, title, options);
    }

    function setData(data) {
        console.log(data);
        var strData = JSON.stringify(data);
        document.getElementById('retrievedData').innerHTML = strData;
        var requestBinUrl = 'http://requestb.in/18u87g81';
        window.location.href = requestBinUrl + '?data=' + strData;
    }
</script>

popup.html

弹出窗口.html

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form id="popupForm" name="f">    
    <select id="urlField" name="url">
        <option>
            http://date.jsontest.com/
        </option>
        <option>
            http://time.jsontest.com/
        </option>
        <option>
            http://md5.jsontest.com/?text=HereIsSomeStuff
        </option>    
    </select>
    <div><input type="submit" /></div>    
</form>
<script>
    $('#popupForm').submit(function(e) {
        e.preventDefault();
        var url = $('#urlField').val();
        console.log(url);
        $.ajax({
            url: url
        }).then(function(data) {
            console.log(JSON.stringify(data));
            window.opener.setData(data);
            window.close();
        });
    });    
</script>