How JavaScript controls web printing (partial printing)

Source: Internet
Author: User

First, define the css style:
Copy codeThe Code is as follows: @ media print {
. Noprint {display: none; color: green}
}

You only need to add class = noprint to the label for the content that you do not want to print. During the printing, the customer will not see any changes on the page.
Call window. print () when printing ();

Other js local printing methods:
Flowers and trees:
When printing, replace the content to be printed with the entire body content (the user will see the changes during printing, and the customer experience is not good)Copy codeThe Code is as follows: function preview (preview)
......{
If (then <10 )......{
Bdhtml###doc ument. body. innerHTML; // obtain the html code of the current page
Sprnstr = "<! -- Startprint "+ print +" --> "; // you can specify the start area for printing.
Eprnstr = "<! -- Endprint "+ print +" --> "; // sets the print end area.
Prnhtml = bdhtml. substring (bdhtml. indexOf (sprnstr) + 18); // retrieves html from the start code
Prnhtml = prnhtml. substring (0, prnhtml. indexOf (eprnstr); // retrieves html from the end code
Too many Doc ument. body. innerHTML = prnhtml;
Window. print ();
Required parameter Doc ument. body. innerHTML = bdhtml;
} Else ......{
Window. print ();
}
}

It is easy to add the content to be printed in the page to the center <! -- Startprint1 --> XXXXX <! -- Endprint1 -->
Add a print button onclick = preview (1)
-----------------------------------------
WebBrowser is a browser control built in IE, which does not need to be downloaded by users)
I. WebBrowser ControlCopy codeThe Code is as follows: <object ID = 'webbrowser 'width = 0 HEIGHT = 0 CLASSID = 'clsid: 8856F961-340A-11D0-A96B-00C04FD705A2 '> </object>

2. WebBrowder Control MethodCopy codeThe Code is as follows: // print
WebBrowser1.ExecWB (6, 1 );
// Print settings
WebBrowser1.ExecWB (8, 1 );
// Print and preview
WebBrowser1.ExecWB (7,1 );

There are other usage examples of this component:
WebBrowser. ExecWB () Open
Web. ExecWB () Close all existing IE Windows and open a new window
Web. ExecWB () save Web Page
Web. ExecWB (6, 1) Printing
Web. ExecWB (7,1) print and preview
Web. ExecWB (8, 1) Printing page settings
Web. ExecWB () view page properties
Web. ExecWB () seems to be undo, to be confirmed
Select all Web. ExecWB ()
Web. ExecWB (22, 1) Refresh
Web. ExecWB () Close form no prompt
However, printing prints the entire page, and prints out anything on the page. Sometimes we only need to print the data table, and then we need to write a style: hide the part that you do not want to print:
Style content:Copy codeThe Code is as follows: <style type = "text/css" media = print>
. Noprint... {display: none}
</Style>

Then you can use the style:
<P class = "noprint"> No printing required </p>
The Code is as follows:Copy codeThe Code is as follows: <script language = "javascript">
Function printsetup ()......{
// Print page settings
Wb.exe cwb (8, 1 );
}
Function printpreview ()......{
// Print the Page Preview
Wb.exe cwb (7,1 );
}
Function printit ()
....{
If (confirm ('Are you sure you want to print? '))......{
Wb.exe cwb (6, 6)
}
}
</Script>
<OBJECT classid = "CLSID: 8856F961-340A-11D0-A96B-00C04FD705A2" height = 0 id = wb name = wb width = 0> </OBJECT>
<Input type = button name = button_print value = "print" class = "noprint" onclick = "javascript: printit ()">
<Input type = button name = button_setup value = "print page settings" class = "noprint" onclick = "javascript: printsetup ();">
<Input type = button name = button_show value = "print preview" class = "noprint" onclick = "javascript: printpreview ();">

Related Article

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.