views:

31

answers:

1

Hi, I've come up with javascript that screen scrape html needed for report and save it on server using ajax call after which I call report that uses previously saved html through pop up. Here is code:

   function SaveAndPrintHtml(htmlToPrint) {
      try {
         $.ajax({
            type: 'POST',
            async: true,
            url: 'temp.aspx?Action=SaveHtmlToPrint',  //SAVE DATA TO PRINT
            data: {
               htmlToPrint: encodeFormData(htmlToPrint)
            },
            dataType: "json",
            success: function() {               
                    try {         
                      var url =  'report.aspx';
                      window.open(url, '_blank'); //OPEN REPORT
                    } catch (e) {
                    }           
            },
            error: handleError
         });
      } catch (ex) {
         alert('Unexpected Error!; \n\r ' + ex.message);
      }

Idea is, when user click print button to pick up html from client send it to server save it and upon successful save open report that use previously saved html, in pop up window. This approach works fine on all browsers tested on both MAC & PC except for Safari on MAC.

Any idea why that might be?

EDIT:

JavaScript code that tries to open a pop-up window when the browser first loads (or unloads) a page will fail.

Taking in consideration above mentioned statement, it might be that Safari treats attempt to open pop-up window after ajax request as initial load/unload page stage and therefore forbids it?

A: 

More than likely, the pop-up blocker is blocking your window.open().

You might have better luck doing something like this:

function SaveAndPrintHtml(htmlToPrint) {
  var $form = $('<form method="post" />');
  $form.attr('target', '_blank');
  $form.attr('action', 'temp.aspx?Action=SaveHtmlToPrint');
  $form.append($("<input type='hidden'/>").val(htmlToPrint);
  // grab the form DOM object and submit it - it should open in a new window:
  $form[0].submit();
}

This should post a form with the proper data to the proper url, opening a new window to do so. Then you make your temp.aspx pass a redirect header to the print.aspx.

gnarf
beautiful, I'll give it a try and inform you if that works.
krul
unfortunately this code is not working for me, I can not post on the fly created form on ff or safari but thanks for the effort
krul