JS printing function

Source: Internet
Author: User

1. window. print mode:
Jsp page printing button:
<Input type = "button" value = "print" onclick = "print ();">
Js:
Function print (){
Window. print ();
In the style, set the hide button to print:
<Style>
@ Media print {
. Noprint {display: none}
}
</Style>
2. WebBrowser Control Mode
WebBrowser is a browser control built in IE, which does not need to be downloaded by users.
I. WebBrowser Control
<Object ID = 'webbrowser 'width = 0 HEIGHT = 0 CLASSID = 'clsid: 8856F961-340A-11D0-A96B-00C04FD705A2 '> </object>
2. WebBrowder Control Method
// 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:
<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:
<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 ();">
3. Remove the header and footer <HTML> <HEAD> <TITLE> web Print to remove the header and footer, and page elements that you do not want to print </TITLE> <META http-equiv = Content-Type content = "text/html; charset = gb2312 "> <SCRIPT language = javascript> function printpr () // preview function {document. all ("qingkongyema "). click (); // remove the header and footer document before printing. all ("dayinDiv "). style. display = "none"; // hide the elements that do not want to print the output before printing (in this example, hide the "print" and "print preview" buttons) var oleateid = 7; var PROMPT = 1; var WebBrowser = '<object id = "WebBrowser1" WIDTH = 0 HEIGHT = 0 CLASSID = "CLSID: 8856F961-340A-11D0-A96B-00C04FD705A2"> </OBJECT>'; document. body. insertAdjacentHTML ('beforeend', WebBrowser); WebBrowser1.ExecWB (oleodiid, PROMPT); WebBrowser1.outerHTML = ""; document. all ("dayinDiv "). style. display = ""; // display this element after printing (the "print" and "print preview" buttons are displayed for others to print them next time)} function printTure () // print the function {document. all ('qingkongyema '). click (); // same as document. all ("dayinDiv "). style. display = "none"; // same as window. print (); document. all ("dayinDiv "). style. display = "";} function doPage () {layLoading. style. display = "none"; // same as above} </SCRIPT> <script language = "VBScript"> dim hkey_root, hkey_path, hkey_key hkey_root = "HKEY_CURRENT_USER" hkey_path = "\ Software \ Microsoft \ Internet Explorer \ PageSetup" '// set the function pagesetup_null () on error resume next Set RegWsh = CreateObject ("WScript. shell ") hkey_key =" \ header "RegWsh. regWrite hkey_root + hkey_path + hkey_key, "" hkey_key = "\ footer" RegWsh. regWrite hkey_root + hkey_path + hkey_key, "" end function '// Set the default function pagesetup_default () on error resume next Set RegWsh = CreateObject ("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" end function </script> </HEAD> <BODY background = "images/background_01.gif" leftMargin = 0 topMargin = 0 rightMargin = 0 bottomMargin = 0 style = "BACKGROUND-POSITION: center 50% "> <DIV align = center> the content you want to print .......... </DIV> <DIV align = "center" id = "dayinDiv" name = "dayinDiv"> <input type = "button" class = "tab" value = "print" onclick = "printTure (); "> <input type =" button "class =" tab "value =" print preview "onclick =" printpr (); "> <input type =" hidden "name =" qingkongyema "id =" qingkongyema "class =" tab "value =" Clear page number "onclick =" pagesetup_null () "> <input type =" hidden "class =" tab "value =" Restore page number "onclick =" pagesetup_default () "> </DIV> </BODY> </HTML> for more questions, please refer to shenzhidao.com)

From the online world

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.