print() doesn\'t work in IE after opening a new window. It works in Chrome. Here\'s a tester:
What worked for me was adding myWindow.document.close()
after myWindow.document.write()
. Here's my solution with a timeout to wait for the new window to finish loading (if you have a lot to load):
var win = window.open('', 'PrintWindow');
win.document.write('Stuff to print...');
setTimeout(function () {
win.document.close();
win.focus();
win.print();
win.close();
}, 1000);
<script type="text/javascript">
function printDiv(divName) {
var printContents = document.getElementById(divName).innerHTML;
var originalContents = document.body.innerHTML;
document.body.innerHTML = printContents;
window.print();
document.body.innerHTML = originalContents;
}
</script>
<div id="printableArea">CONTENT TO PRINT</div>
<input type="button" onclick="printDiv('printableArea')" value="Print Report" />
function printCrossword(printContainer) {
var DocumentContainer = getElement(printContainer);
var WindowObject = window.open('', "PrintWindow", "width=5,height=5,top=200,left=200,toolbars=no,scrollbars=no,status=no,resizable=no");
WindowObject.document.writeln(DocumentContainer.innerHTML);
WindowObject.document.close();
WindowObject.focus();
WindowObject.print();
WindowObject.close();
}
try this
<html>
<head>
<script type="text/javascript">
function openWin()
{
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("<p>This is 'myWindow'</p>");
myWindow.focus();
print(myWindow);
}
</script>
</head>
<body>
<input type="button" value="Open window" onclick="openWin()" />
</body>
</html>
checkout: window.print() not working in IE
Working sample: http://jsfiddle.net/Q5Xc9/1/
Turgut gave the right solution. Just for clarity, you need to add close after writing.
function openWin()
{
myWindow=window.open('','','width=200,height=100');
myWindow.document.write("<p>This is 'myWindow'</p>");
myWindow.document.close(); //missing code
myWindow.focus();
myWindow.print();
}