6

I'm using jQuery to open a popup and I'd like to send it data using the post method when it opens. Can anyone help me, thanks in advance.

I am currently passing the data using the get method so the data is a part in url, but I don't want the data to be visible in the url.

function openWindow(){

    var name = $('#name').val();

    var url = 'popup_window.php?name='+name;
    window.open(
        url,
        'popUpWindow',
        'height=400,     \
         width=650,      \
         left=300,       \
         top=100,        \
         resizable=yes,  \
         scrollbars=yes, \
         toolbar=yes,    \
         menubar=no,     \
         location=no,    \
         directories=no, \
         status=yes');
}
Pramod
  • 2,828
  • 6
  • 31
  • 40

1 Answers1

9

This is based on the answer in How to open popup and populate it with data from parent window?

var newpage;
function openWindow() {
    $.post('popup_window.php', {name: $('#name').val()}, function(result) {
        newpage = result;
        window.open('Popup.html', 'popUpWindow','height=400, width=650, left=300, top=100, resizable=yes, scrollbars=yes, toolbar=yes, menubar=no, location=no, directories=no, status=yes');
    });
}

Popup.html should contain:

<script type="text/javascript">
    if(window.opener && !window.opener.closed) {
        document.write(window.opener.newpage);
    }
</script>
Community
  • 1
  • 1
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 2
    I think you can reduce this to: window.open('javascript: document.write(window.opener.newpage);'.... and you will not need the Popup.html at all. – Sych Jun 18 '14 at 16:44
  • I am also having same problem, tried your method but problem is it first loads data then open window, which makes slow response, i want it first open window then load data, what should i do? – Imran Qamer Jan 05 '16 at 08:34
  • If the post response is a file download (csv)...is there a way to to that instead of just displaying the file contents in the new window? – AS7K Aug 30 '17 at 15:48
  • @AS7K I don't think you can do that in the post response. Javascript can't download files by itself. It would have to be done by `Popup.html`, which can send `Content-Disposition: attachment` to start a download. – Barmar Aug 30 '17 at 23:28