开发者

window.print is not working for a page that has been rendered by document.write

Scen开发者_C百科ario: I'm creating a new window (new tab) and writing html markup to it via javascript. 1. new document. 2. write html markup to document's content. 3. show the new document.

What I need to do: I basically need to print a simple HTML page (javascript print dialog) by clicking on a button, link, or onload. The problem, is i cant get either way to work... with one exception.

(the one exception) Using the onload="window.print()" will only work if the new window appears and i click refresh (the print dialog wont display upon initial creation of the window).

The weird thing is, even if i invoke "window.print()" on event clicks (href, button...etc) nothing happens. But I know it's not the browser because samples/demos of this SIMPLE thing works just fine.

So it seems it's somehow related to my page being rendered the way it is (described in the "Scenario" section of this).

CODE: When i click on a Print button, i call this (right now it is not important exactly who/what calls this. but just know that it is called):

function Print(htmlContent) {
  var printWindow = window.open();
  var printDoc = printWindow.document;
  printDoc.write(htmlContent);

And i forgot to mention this: The HTML (htmlContent) that is passed to the Print scrip function is indeed Html, but it has been transformed by an XSL. Here is the XSL:

<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
    <head>
        <style type="text/css" media="print">
            .noprint{
                    display:none;
                }
        </style>
        <script type='text/javascript'>
            function startPrint(){
                window.print();
            }
        </script>
    </head>

      <body>        
        <div >
            <a id="printButton" href="startPrint();">Print this page</a>
        </div>
        Hello world.
</body>
    </html>
  </xsl:template>
</xsl:stylesheet>

I can confirm javascript works because if i replace the href="startPrint()" with href="alert('hi')", i do indeed get a popup with that alert.


I am still not clear with what actually you need to do with this code. But, from that I could understand & as per the reference available here, I have implemented this.

This is working fine as you have mentioned regarding javascript print dialog onClick of link. But, still I think this doesn't seem your solution. Please correct me with my error.

Thank You.

For reference : w3schools

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜