1

I am trying to open a new window and print the contents.

var parentHeader= $("head").html();
var parentDivContent = $("#printer_data_1").html();
var newW= window.open("", "", "scrollbars=1,width=200,height=200");
    newW.document.write('<html><head><title>Title</title>'+parentHeader+'</head>');
    newW.document.write('<body><div>');
    newW.document.write(parentDivContent );
    newW.document.write('</div></body></html>');
    newW.document.close();
    newW.print();
    newW.close();

When i try to run the above code, it hangs in IE8, when i comment the below line... it works.

newW.document.write('<html><head><title>Title</title>'+parentHeader+'</head>');

I want the entire content, styles to be loaded in the head section of new window.

Shane
  • 5,517
  • 15
  • 49
  • 79
  • I suggest you have a look at the third answer on this URL: http://stackoverflow.com/questions/2555697/window-print-not-working-in-ie – EugenSunic Jul 04 '15 at 00:01

2 Answers2

2

want the entire content, styles to be loaded in the head section of new window

Try compiling html for newW into single string , including head html parentHeader ; setting name reference for newW at second parameter of call to window.open() ; calling document.write with single single html string as parameter

var parentHeader= $("head").html();
var parentDivContent = $("#printer_data_1").html();
var newW= window.open("", "newW", "scrollbars=1,width=200,height=200");
var html = "<html>" 
           + parentHeader // `parentHeader` `head` `html`
           + "<body><div>" 
           + parentDivContent 
           + "</div></body></html>"
newW.document.write(html);
newW.document.close();
newW.print();
newW.close();
guest271314
  • 1
  • 15
  • 104
  • 177
1

You may want to use innerHTML instead of document.write. That could be your issue.

var someVar = 50,
  el = document.getElementById('btn');

function onOpen() {
  var prnt = open();
  prnt.document.title = 'Page Title';
  prnt.document.body.innerHTML = [
    '<h1>',
    'heading of the page',
    someVar,
    '</h1>',
    '<p>A paragraph with text.</p>'
  ].join('');
  prnt.print();
  prnt.close();
}

el.attachEvent('onclick', onOpen);
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <title>Print for Legacy IE</title>
</head>

<body>

  <a id="btn" href="#">Link</a>

</body>

</html>
colecmc
  • 3,133
  • 2
  • 20
  • 33