Shane
Shane

Reputation: 5677

window.print() hangs in internet explorer

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.

Upvotes: 1

Views: 909

Answers (2)

colecmc
colecmc

Reputation: 3318

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>

Upvotes: 1

guest271314
guest271314

Reputation: 1

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();

Upvotes: 2

Related Questions