Mohammad Adnan
Mohammad Adnan

Reputation: 6627

printing page as popup window is not working on chrome working on mozilla and IE

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".

Plunker demo

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

Answers (3)

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

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

Mohammad Adnan
Mohammad Adnan

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

user5105916
user5105916

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

Related Questions