How to pass data to parent window from popup window?









up vote
14
down vote

favorite
2












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.










share|improve this question





















  • What do you mean with popup, do you mean a window opened with _blank?
    – sQVe
    Aug 26 '12 at 9:51











  • window.ppopup()
    – Click Upvote
    Aug 26 '12 at 10:50














up vote
14
down vote

favorite
2












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.










share|improve this question





















  • What do you mean with popup, do you mean a window opened with _blank?
    – sQVe
    Aug 26 '12 at 9:51











  • window.ppopup()
    – Click Upvote
    Aug 26 '12 at 10:50












up vote
14
down vote

favorite
2









up vote
14
down vote

favorite
2






2





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.










share|improve this question













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.







javascript jquery popup






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 26 '12 at 9:47









Click Upvote

95.1k222508688




95.1k222508688











  • What do you mean with popup, do you mean a window opened with _blank?
    – sQVe
    Aug 26 '12 at 9:51











  • window.ppopup()
    – Click Upvote
    Aug 26 '12 at 10:50
















  • What do you mean with popup, do you mean a window opened with _blank?
    – sQVe
    Aug 26 '12 at 9:51











  • window.ppopup()
    – Click Upvote
    Aug 26 '12 at 10:50















What do you mean with popup, do you mean a window opened with _blank?
– sQVe
Aug 26 '12 at 9:51





What do you mean with popup, do you mean a window opened with _blank?
– sQVe
Aug 26 '12 at 9:51













window.ppopup()
– Click Upvote
Aug 26 '12 at 10:50




window.ppopup()
– Click Upvote
Aug 26 '12 at 10:50












2 Answers
2






active

oldest

votes

















up vote
23
down vote



accepted










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() 





share|improve this answer




















  • will not work in IE when popup window is Cross Domain .
    – Salman
    Mar 19 '15 at 8:46










  • I see error: Permission denied to access property yourFunc()
    – 123qwe
    Aug 18 '15 at 10:37










  • stackoverflow.com/a/32617334/470749 is a good example of this working.
    – Ryan
    Aug 18 '16 at 6:31










  • 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 at 7:09


















up vote
3
down vote













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>





share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12129121%2fhow-to-pass-data-to-parent-window-from-popup-window%23new-answer', 'question_page');

    );

    Post as a guest






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    23
    down vote



    accepted










    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() 





    share|improve this answer




















    • will not work in IE when popup window is Cross Domain .
      – Salman
      Mar 19 '15 at 8:46










    • I see error: Permission denied to access property yourFunc()
      – 123qwe
      Aug 18 '15 at 10:37










    • stackoverflow.com/a/32617334/470749 is a good example of this working.
      – Ryan
      Aug 18 '16 at 6:31










    • 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 at 7:09















    up vote
    23
    down vote



    accepted










    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() 





    share|improve this answer




















    • will not work in IE when popup window is Cross Domain .
      – Salman
      Mar 19 '15 at 8:46










    • I see error: Permission denied to access property yourFunc()
      – 123qwe
      Aug 18 '15 at 10:37










    • stackoverflow.com/a/32617334/470749 is a good example of this working.
      – Ryan
      Aug 18 '16 at 6:31










    • 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 at 7:09













    up vote
    23
    down vote



    accepted







    up vote
    23
    down vote



    accepted






    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() 





    share|improve this answer












    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() 






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Aug 26 '12 at 9:52









    Hoff

    17.5k155680




    17.5k155680











    • will not work in IE when popup window is Cross Domain .
      – Salman
      Mar 19 '15 at 8:46










    • I see error: Permission denied to access property yourFunc()
      – 123qwe
      Aug 18 '15 at 10:37










    • stackoverflow.com/a/32617334/470749 is a good example of this working.
      – Ryan
      Aug 18 '16 at 6:31










    • 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 at 7:09

















    • will not work in IE when popup window is Cross Domain .
      – Salman
      Mar 19 '15 at 8:46










    • I see error: Permission denied to access property yourFunc()
      – 123qwe
      Aug 18 '15 at 10:37










    • stackoverflow.com/a/32617334/470749 is a good example of this working.
      – Ryan
      Aug 18 '16 at 6:31










    • 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 at 7:09
















    will not work in IE when popup window is Cross Domain .
    – Salman
    Mar 19 '15 at 8:46




    will not work in IE when popup window is Cross Domain .
    – Salman
    Mar 19 '15 at 8:46












    I see error: Permission denied to access property yourFunc()
    – 123qwe
    Aug 18 '15 at 10:37




    I see error: Permission denied to access property yourFunc()
    – 123qwe
    Aug 18 '15 at 10:37












    stackoverflow.com/a/32617334/470749 is a good example of this working.
    – Ryan
    Aug 18 '16 at 6:31




    stackoverflow.com/a/32617334/470749 is a good example of this working.
    – Ryan
    Aug 18 '16 at 6:31












    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 at 7:09





    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 at 7:09













    up vote
    3
    down vote













    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>





    share|improve this answer


























      up vote
      3
      down vote













      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>





      share|improve this answer
























        up vote
        3
        down vote










        up vote
        3
        down vote









        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>





        share|improve this answer














        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>






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 23 '17 at 11:53









        Community

        11




        11










        answered Aug 18 '16 at 7:10









        Ryan

        8,639761146




        8,639761146



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f12129121%2fhow-to-pass-data-to-parent-window-from-popup-window%23new-answer', 'question_page');

            );

            Post as a guest














































































            Popular posts from this blog

            Darth Vader #20

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Ondo