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?

Dušan Rychnovský
  • 11,699
  • 8
  • 41
  • 65
  • 1
    Possibly related to [this question](http://stackoverflow.com/questions/6690598/permission-denied-to-access-property-in-iframe), it might be a problem of [same origin policy](http://en.wikipedia.org/wiki/Same_origin_policy). – Miklos Aubert Aug 30 '13 at 08: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. – Dušan Rychnovský Aug 30 '13 at 09:04

2 Answers2

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

}

coolprarun
  • 1,153
  • 2
  • 15
  • 22
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.

LuigiEdlCarno
  • 2,410
  • 2
  • 21
  • 37