Dušan Rychnovský
Dušan Rychnovský

Reputation: 12499

Printing a page using javascript does not work

The following HTML page should open http://www.seznam.cz in a new window and then open the Print dialog to allow printing it.

<html>
    <head>
        <script>
            function printPage() 
            {
                var newWindow = window.open("http://www.seznam.cz", "seznam");
                newWindow.document.close();
                newWindow.focus();
                newWindow.print();
                newWindow.close();
            }
        </script>
    </head>
    <body>
        <a onClick="printPage(); return false;" href="">Print</a>
    </body>
</html>

However it only prints a blank page with "about:blank" in the top right and the current date and time in the bottom right corner.

Why doesn't it work as expected?

Upvotes: 0

Views: 676

Answers (2)

coolprarun
coolprarun

Reputation: 1163

Try this one.. it will print the Page.You can Change it to Your Requirements.

<input type="button" value="Print" onclick="printPage('content');"></input>

function printPage(id)
{
   var html="<html>";
   html+= document.getElementById(id).innerHTML;
   html+="</html>";

   var printWin = window.open('','','left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status  =0');
   printWin.document.write(html);
   printWin.document.close();
   printWin.focus();
   printWin.print();
   printWin.close();

}

Upvotes: 1

LuigiEdlCarno
LuigiEdlCarno

Reputation: 2415

The reason is, because you close the window again in you function. Remove

newWindow.document.close();
newWindow.close();

The reason for the blank page is, because you have specified an empty string as href value. Remove the hrefattribute from the tag.

Upvotes: 0

Related Questions