js window.open then print()

153,183

Solution 1

checkout: window.print() not working in IE

Working sample: http://jsfiddle.net/Q5Xc9/1/

Solution 2

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

Solution 3

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

Solution 4

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

Solution 5

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();
}
Share:
153,183

Related videos on Youtube

Clive
Author by

Clive

I'm a Web Developer with 10+ years of experience with C#, Javascript, ASP .NET, Classic ASP, HTML, CSS, Sql Server &amp; MySql. I've kept up-to-date with the latest developments in ASP .NET and I'm currently working with MVC 4, Web API, EF 5, Knockout JS, Kendo UI &amp; Twitter Bootstrap. I've also worked with hybrid Web Forms and MVC projects. Recent interests include No Sql, Parallel Programming (Rx, Task, async/await) and Single Page Applications (Knockout / Breeze). Recent Websites: http://www.justdo1thing.com http://www.weddingplanner.co.uk http://www.appinstruct.com http://support.cwsindustries.ca/ http://www.cadburygiftsdirect.co.uk http://www.trutechdoors.com http://www.greenandblacks.co.uk http://www.wealthadvisorcrm.com

Updated on July 09, 2022

Comments

  • Clive
    Clive almost 2 years

    print() doesn't work in IE after opening a new window. It works in Chrome. Here's a tester:

    <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();
        myWindow.print(); //DOES NOT WORK
      }
    </script>
    </head>
    <body>
    
    <input type="button" value="Open window" onclick="openWin()" />
    
    </body>
    </html>
    
  • skmaran.nr.iras
    skmaran.nr.iras almost 12 years
    Can you please look at this stackoverflow.com/questions/11448910/…
  • user3340627
    user3340627 almost 9 years
    The only solution that worked for me in IE-11. Thanks !!
  • Miguel
    Miguel about 8 years
    On my chrome, the window stays open even after closing the printing window
  • Kadaiser
    Kadaiser about 3 years
    It´s working indeed, but cannot find why win.document.close() it's need it.