0

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?

2
  • 1
    Possibly related to this question, it might be a problem of same origin policy. Commented Aug 30, 2013 at 8:46
  • Yes I guess it really is a problem of the same origin policy after all. That's why @collprarun's version works and the original one does not. Commented Aug 30, 2013 at 9:04

2 Answers 2

1

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

}

Sign up to request clarification or add additional context in comments.

1 Comment

I think you loss <script type="text/javascript">...</script>
0

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.

1 Comment

Unfortunately this does not work. Even after I removed those two lines the result is still the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.