Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery post, response in new window

I have a script that on.DocumentReady posts data to another page. That page responds with some HTML encapsulated in one div tag.

My goal is to have this post response/data open in a new window.

Any hints or clues?

Here is the snippet I created from Dr. Mille's advice.

 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js"></script>
 <script type="text/javascript">
$(document).ready(function() {
var packslip_id = 35592;
var po_no = 0018439;
var  box_no = 1;
    $.post("https://example.com/barcode/generate", { packing_slip: packslip_id, reference: po_no, total_boxes: box_no}, 
    function (data) {
        alert(data);
        var win=window.open('about:blank');
        with(win.document)
        {
            open();
            write(data);
            close();
        }
    });
 });

like image 513
Peter Avatar asked Sep 29 '10 19:09

Peter


2 Answers

If you dont need a feedback about the requested data and also dont need any interactivity between the opener and the popup, you can post a hidden form into the popup:

Example:

<form method="post" target="popup" id="formID" style="display:none" action="https://example.com/barcode/generate" >
  <input type="hidden" name="packing_slip" value="35592" />
  <input type="hidden" name="reference" value="0018439" />
  <input type="hidden" name="total_boxes" value="1" />
</form>
<script type="text/javascript">
window.open('about:blank','popup','width=300,height=200')
document.getElementById('formID').submit();
</script>

Otherwise you could use jsonp. But this works only, if you have access to the other Server, because you have to modify the response.

like image 173
Dr.Molle Avatar answered Nov 17 '22 11:11

Dr.Molle


Accepted answer doesn't work with "use strict" as the "with" statement throws an error. So instead:

$.post(url, function (data) {
    var w = window.open('about:blank', 'windowname');
    w.document.write(data);
    w.document.close();
});

Also, make sure 'windowname' doesn't have any spaces in it because that will fail in IE :)

like image 44
Rocklan Avatar answered Nov 17 '22 10:11

Rocklan