JavaScript的打印技术体会

1.打印某个frame的内容(frameB)
    <html>
    <frameset rows="20%,*">
        <frame SRC="framea.htm" name="FrameA" noresize>
        <frame SRC="frameb.htm" name="FrameB" noresize>
    <noframes>
    <body>
    </body>
    </noframes>
    </frameset>
    </html>

frameA中添加
    <input type="button" value="Print the other frame" onclick="parent.frameB.print();">

2.打印某个对象中的内容,如textbox中的内容
    frameA:
    <html><head>
     </head>
    <body>
    <form name="formB" action="javascript:window.print()">
        <input type="text" name="textboxb" size="12" value="Print Me">
    </form>
    </body></html>
frameB:
    <html><head>
    <script>
    function printotherframe()
    {
        parent.FrameB.formB.textboxb.focus()
        parent.FrameB.formB.submit()
    }
    </script>
    </head><body>
        <form name="formA"><input type="button" value="Print the other frame" onclick="printotherframe()">
    </body></html>

3.打印非当前页
技术同上,只是在frame显示中动一点点骗人的手脚
    <frameset rows="100%,*">
    <frame src="controlpage.html">
    <frame src="pagetoprint.html">
    </frameset>

    <script language="JavaScript"><!--
        if (window.print)
        document.write('<form><input type="button" value="Print" onClick="parent.frames[1].focus();parent.frames[1].print()"><\/form>');
    //--></script>

4.统一页眉页脚
    <script language="Javascript"><!--
    function doprint() {
      //save existing user's info
      var h = factory.printing.header;
      var f = factory.printing.footer;
      //hide the button
      document.all("printbtn").style.visibility = 'hidden';
      //set header and footer to blank
  // -- advanced features 
  factory.printing.SetMarginMeasure(2) // measure margins in inches 
  factory.SetPageRange(false, 1, 3) // need pages from 1 to 3 
  factory.printing.printer = "HP DeskJet 870C" 
  factory.printing.copies = 2 
  factory.printing.collate = true 
  factory.printing.paperSize = "A4" 
  factory.printing.paperSource = "Manual feed" //付费才能用。

      factory.printing.header = "";
      factory.printing.footer = "";
      factory.printing.portrait=true; // factory.printing.portrait = false; 横,纵纸张设置
      //print page without prompt
      factory.DoPrint(false);
      //restore user's info
      factory.printing.header = h;
      factory.printing.footer = f;
      //show the print button
      document.all("printbtn").style.visibility = 'visible';
    }
    //--></script>
    <body>
    <br><br><br>
    <object id=factory style="display:none"  classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" viewastext codebase="ScriptX.cab#Version=5,0,4,185">
    </object>
    <br><br>
    <div id="printbtn"><input name=idPrint type=button value="Print the letter" onclick="doprint()"></div>
    </body>

5.打开新窗口并自动打印
    <object id=WBControl width=0 height=0 classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2></object>
    <script language=VBScript><!-- //
     Sub VBPrint() On Error Resume Next
        WBControl.ExecWB 6,1    '7,2在不少文章中都有提到,这里就不多说了。
     End Sub
    //--></script>

    <script language=JavaScript><!-- //
    if (window.print) self.print();
    else if (navigator.appName.indexOf('Microsoft') !=-1) VBPrint()
    setTimeout('self.close()',3000);    '看代码相信大家都能明白。如果有不明白的欢迎留言
    //--></script>
html中
    <form>
    <input type="button" value="Print Price List" onClick="window.open('pricelist.html','newwin');">
    </form>

//设置在body onload中时,系统将自动恢复原来的打印设置

 


你可能感兴趣的:(JavaScript)