Reputation: 6627
I am trying to print a page as popup window. For Mozilla and IE its working but on chrome a popup window appears but it gives "print preview failed".
And portion of javascript is -
function PrintElem(elem) {
console.log($(elem).html())
Popup($( elem).html());
}
function Popup(data)
{
var printContent = data;
var disp_setting="toolbar=no,location=no,directories=no,menubar=no, scrollbars=no,width=600, height=800"
var myWindow = window.open("","",disp_setting);
myWindow.document.write(printContent);
myWindow.document.close();
myWindow.focus();
myWindow.print();
myWindow.close();
return true;
}
Not sure why Chrome is not happy with my script.
Upvotes: 0
Views: 3195
Reputation: 167172
If you are using JQuery, you can use $(document).ready(function () {});
. Have a look at this here:
$(function () {
window.print();
window.close();
});
Upvotes: 2
Reputation: 6627
Thanks A.Wolff for your answer and VennilaSundarRajan for providing insight about issue - I am posting here on this behalf. The real problem was time rendering time by page. use this-
myWindow.onload = function(){ this.print(); this.close(); }
Upvotes: 0
Reputation:
The setTimeout
of 500
milliseconds worked:
function Popup(data)
{
var printContent = data;
var disp_setting="toolbar=no,location=no,directories=no,menubar=no, scrollbars=no,width=600, height=800";
var myWindow = window.open("","",disp_setting);
myWindow.document.open();
myWindow.document.write(printContent);
myWindow.document.close();
myWindow.focus();
setTimeout(function () {
myWindow.print();
myWindow.close();
}, 500);
return true;
}
Upvotes: 3