Saving html on servers side prior to opening report that use that saved html doesn't work on Safari
I've come up with Javascript that screen scrapes html needed for a 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?
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
.
精彩评论