2013-08-30 126 views
0

以下HTML页面应在新窗口中打开http://www.seznam.cz,然后打开“打印”对话框以允许打印。使用javascript打印页面不起作用

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

但是它只打印一个空白页,右上角显示“about:blank”,右下角显示当前日期和时间。

为什么不按预期工作?

+1

可能与[这个问题](http://stackoverflow.com/questions/6690598/permission-denied-to-access-property-in-iframe),它可能是一个[相同来源政策]的问题(http://en.wikipedia.org/wiki/Same_origin_policy)。 –

+0

是的,我想这毕竟是一个相同的起源政策的问题。这就是为什么@ collprarun的版本起作用,而原来的版本不起作用。 –

回答

1

试试这个..它会打印Page.You可以更改为您的要求。

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

}

+1

我觉得你损失'' –

+0

哦,谢谢你的纠正@AldiUnanto – coolprarun

0

原因是,因为您在功能中再次关闭窗口。删除

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

的原因空白页是,因为你已经指定了一个空字符串作为href值。从标签中删除href属性。

+0

不幸的是,这是行不通的。即使我删除了这两行,结果仍然是一样的。 –