Javascript web page Printing

Source: Internet
Author: User

General printing (full page printing) printing part of the content in the web page (custom) print remove/Add Header/footer use external controls/method to achieve multi-function Print print background the above is the code control set "page settings" to achieve print parameter settings (Window system graphic version) i. general printing (the whole page is printed). This is needless to say and can be referenced directly: window. print (); 2. Printing part of the content in the web page (custom) is implemented in three ways. 1. Use css to Control Reference: @ media print. a {display: block }. B {display: hidden} sets the class you do not want to print as B. First, add the following reference to the webpage: <OBJECT id = "WebBrowser" height = "0" width = "0" classid = "CLSID: 8856F961-340A-11D0-A96B-00C04FD705A2" VIEWASTEXT> </OBJECT> then you can add the function buttons in sequence: reference: <input typ E = "button" value = "print"> <input type = "button" value = "print"> <input type = "button" value = "page settings"> <input type = "button" value = "print preview"> <INPUT type = "button" value = "Close Window"> place the two items in <center class = noprint> </center> will not print these buttons. Of course, you must define noprint: <style media = "print">. Noprint {DISPLAY: none} </style>. You only need to set the css Of the things you don't want to print to noprint. Now we have implemented basic web printing. Note the following:. activeX controls that are not marked as secure must be set in internet Options security settings of ie... set to prompt or enable. Otherwise, an error is reported, resulting in unavailability. B. if you edit the page in the vs.net editing environment, it automatically adds additional parameters to the object. With these parameters, printing will fail, so remember to delete them when saving them. 2. Use javascript to print the content in a fixed tag. Add JavaScript to the Code header of the page. Reference: <script language = javascript> function doPrint () {bdhtml##doc ument. body. innerHTML; sprnstr = "<! -- Startprint --> "; eprnstr =" <! -- Endprint --> "; prnhtml = bdhtml. substr (bdhtml. indexOf (sprnstr) + 17); prnhtml = prnhtml. substring (0, prnhtml. indexOf (eprnstr); Specify your doc ument. body. innerHTML = prnhtml; window. print () ;}</script> 2. Add <! -- Startprint --> and <! -- Endprint --> ID. That is, append it to the html corresponding to the text to be printed and saved by the user. At the same time, if you use a thief program to obtain remote data and print it, you can place the data within the defined tag. 3. The part of the captured content has been completed. Add a "print" link: XML/HTML code <a href = "javascript:;" onClick = "doPrint () "> Print </a> reference: <script language =" JavaScript "> var hkey_root, hkey_path, hkey_keyhkey_root = "HKEY_CURRENT_USER" hkey_path = "\ Software \ Microsoft \ Internet Explorer \ PageSetup \" // set the function pagesetup_null () {try {var RegWsh = new ActiveXObject ("WScript. shell ") hkey_key =" header "RegWsh. regWrite (hkey_root + hkey_path + hkey_key, "") Hkey_key = "footer" RegWsh. regWrite (hkey_root + hkey_path + hkey_key, "")} catch (e) {}} // set the default function pagesetup_default () for the header and footer of the web page () {try {var RegWsh = new ActiveXObject ("WScript. shell ") hkey_key =" header "RegWsh. regWrite (hkey_root + hkey_path + hkey_key, "& w & B page number, & p/& P") hkey_key = "footer" RegWsh. regWrite (hkey_root + hkey_path + hkey_key, "& u & B & d")} catch (e) {}}</script> <input type = "button" value = "Clear page number" onclick = pagesetup_n Ull ()> <input type = "button" value = "Recover page number" onclick = pagesetup_default ()> copy it out and check the effect. (4) use external controls/methods to achieve multi-functional printing 1. IEWebBrowser component introduction http://support.microsoft.com/default.aspx? Scid = kb % 3BEN-US % 3BQ267240 # top http://support.microsoft.com/kb/q247671/#appliesto code reference: <OBJECT classid = CLSID: 8856F961-340A-11D0-A96B-00C04FD705A2 height = 0 id = WebBrowser width = 0> </OBJECT> <input name = Button onClick = document. all. webBrowser. execWB (1, 1) type = button value = open> <input name = Button onClick = document. all. webBrowser. execWB (2, 1) type = button value = close all> <input name = Button onClick = document. all. we BBrowser. execWB (4,1) type = button value = Save As> <input name = Button onClick = document. all. webBrowser. execWB (6, 1) type = button value = print> <input name = Button onClick = document. all. webBrowser. execWB (6, 6) type = button value = direct print> <input name = Button onClick = document. all. webBrowser. execWB (7,1) type = button value = print preview> <input name = Button onClick = document. all. webBrowser. execWB (8, 1) type = button value = page Settings> <input nam E = Button onClick = document. all. webBrowser. execWB (10, 1) type = button value = property> <input name = Button onClick = document. all. webBrowser. execWB (17,1) type = button value = select all> <input name = Button onClick = document. all. webBrowser. execWB (22, 1) type = button value = Refresh> <input name = Button onClick = document. all. webBrowser. execWB (45, 1) type = button value = off> 2. Use ScriptX. the cab control uses the object element to modify the value of codebase and classid. The control ScriptX is called here. ca B code reference: <OBJECT id = "factory" style = "DISPLAY: none" codeBase = "$ {rootUrl} js/smsx. cab # VVersion = 6, 3, 435,20 "classid =" clsid: 1663ed61-23eb-11d2-b92f-008048fdd814 "viewastext> </OBJECT> This code is used to load the cab file, clsid and codebase must correspond to the information in the downloaded cab. Otherwise, the component will be loaded incorrectly. It is not difficult to find these two items. As long as you use winrar to open the downloaded cab file, and find the extension. inf file, and then open it, you can see. 3. Call the code reference of the Print. js file of the control Script: function setPrintBase (headerText, footerText, rootUrl) {// -- advanced features, which has never been used and is awaiting confirmation. // 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" var header = (headerText = null | headerText = "")? 'Default head': headerText; var footer = (footerText = null | footerText = "")? 'Plain page': footerText; factory. printing. header = "& B" + header + "& B"; factory. printing. footer = "& B" + footer; factory. printing. portrait = true; factory. printing. leftMargin = 10.00; factory. printing. topMargin = 10.00; factory. printing. rightMargin = 10.00; factory. printing. example reference:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.