23

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.

Ali
  • 261,656
  • 265
  • 575
  • 769

2 Answers2

33

The window.opener object 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.yourFunc() 
Hoff
  • 38,776
  • 17
  • 74
  • 99
  • 1
    will not work in IE when popup window is Cross Domain . – Salman Mar 19 '15 at 08:46
  • 1
    I see error: Permission denied to access property yourFunc() – 123qwe Aug 18 '15 at 10:37
  • Old post, but window.parent.document.getElementById('panelControlId') worked for me. It provided the handle to the panel holding this child window, thereby allowing to call pnl.style.display = 'none'; on the panel, thus collapsing it. – DiligentKarma May 23 '18 at 07:09
5

Here is a fun and easy demo that 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:

parent.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

<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>
Community
  • 1
  • 1
Ryan
  • 22,332
  • 31
  • 176
  • 357