Javascript control web page Printing

Source: Internet
Author: User

General printing (full page printing) printing part of the content in the web page (custom) Printing remove/Add a header and footer using external controls/methods to achieve multi-function printing print background

The above is code control

Set "page settings" to set printing parameters (Windows system graphic version)

I. general printing (full page printing)
This can be used directly.

Reference: window. print ();

Ii. print some content in the webpage (custom)

Three methods
1. Use css to control

Reference: @ media print
. A {display: block}
. B {display: hidden}
Set part of the class you don't want to print to B.
First, add the following content to the webpage:

Reference: <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 type = "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">
Put the two items in <center class = noprint> </center> and the buttons will not be printed. Of course we need to 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:
A. You must set ActiveX controls that are not marked as secure in the security settings of internet Options of ie to prompt or enable them. Otherwise, an error will be 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 content in a fixed tag
A. 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 ));
Too many 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_key
Hkey_root = "HKEY_CURRENT_USER"
Hkey_path = "SoftwareMicrosoftInternet assumerpagesetup"
// Set the page header and footer to be empty.
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 page header and footer to the default value.
Function pagesetup_default (){
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_null ()>
<Input type = "button" value = "Restore page number" onclick = pagesetup_default ()>


Copy the file and check the effect.

(4) use external controls/methods to implement multi-function 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. WebBrowser. 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 = print directly>
<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 name = Button onClick = document. all. WebBrowser. ExecWB (10, 1) type = button value = attribute>
<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 = close>

 

2. Use the ScriptX. cab Control


1. Download The ScriptX. cab Control

Official http://www.meadroid.com/scriptx/index.asp

2. Use the object element to modify the value of codebase and classid.

The ScriptX. cab control is called here.

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. The clsid and codebase must correspond to the information in the downloaded cab. Otherwise, the component will be loaded incorrectly, as long as you use winrar to open the downloaded cab file and find that the extension is. inf file, and then open it, you can see.

3. Call the control script

Print. js File
Code

Reference:

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 = "")? 'Character 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. bottomMargin = 10.00;

}


Example

Reference:

<Html>
<Head>
<Meta http-equiv = "imagetoolbar" c>
<Script language = "javascript" src = "print. js"> </script>
<Style media = "print">
. Noprint {DISPLAY: none ;}
</Style>
<Title> Print test </title>
</Head>
<OBJECT id = "factory" style = "DISPLAY: none" codeBase = "smsx. cab # VVersion = 6, 3, 435,20" classid = "clsid: Hangzhou" viewastext> </OBJECT>
<Script defer>
Function window. onload (){
SetPrintBase ('header ', 'footer ');
}
</Script>
<Body topmargin = "0" leftmargin = "0" rightmargin = "0" bottommargin = "0" marginwidth = "0" marginheight = "0">
<Center class = "Noprint">
<Input type = button value = "print">
<Input type = button value = "page settings">
<Input type = button value = "print preview">
<Input type = "button" value = "close">
</Center>
<Center>
<Table width = "100%" border = "0" cellpadding = "0" cellspacing = "0">
<Tr> <td align = "center"> <B> content </B> </td> </tr>
</Table>
</Center>
</Body>
</Html>

 

(5) print the background
By default, when you use IE to print a webpage, the webpage background is not printed. To print the webpage background, use the following method:
1. Start IE browser.
2. Select Internet options from the Tools menu to open the Internet Options dialog box.
3. Open the Advanced tab and select the print background color and image check box in the print selection area.
4. Click OK to close the dialog box.

(6) how to print web pages with IE


Internet Explorer (IE) 5.0 and later versions provide powerful print and print preview functions. This article describes how to use IE to print web pages. (Who? Who? Who is throwing tomatoes? Below is a piece of Buzz: Do you still need to talk about it ?!) Yes, it's too easy to print web pages in IE. Click "print" on the toolbar to print the current webpage. As shown in:


However, what is the effect you have achieved? I can't say that. What I want to talk about today is some printing settings. If you set these parameters, you will find that the print function of IE can catch up with Word. In the future, when you print a webpage, you will be able to print it as soon as you want. Well, let's just talk about it. Now let's take a look at how to set the print parameters for IE.
Let's take the student list to be printed ("Student Status Management System MX" query result webpage) as an example to explain how to set these print parameters:


If we click the "print" button in the toolbar or click "print" in the "file" menu to print the webpage, a lot of unnecessary information will be printed. Therefore, we should first use the "print preview" function added by IE5 to view the actual printing effect of the current webpage before printing. As shown in:


In the "print preview" dialog box shown in, we can see that the information we do not need, such as the webpage title, webpage URL address, and printing date, is also printed. The page number is in the upper right corner. Can I move it to another location (such as the lower right corner?


Can we print the webpage according to the style we want? Of course. Everything is in "page settings.

In the displayed "page settings" dialog box, all the secrets are here!


Originally, IE automatically added unnecessary print information to the header and footer. If we don't want any headers and footers, just delete them. :-) Most of the time I do this. But what if you want to customize the header and footer? What exactly do the "& w & B & p" above mean? Just give you a table. Description
& W webpage title
& U webpage address (URL)
& D short date format (specified by "region Settings" in "Control Panel)
& D long Date Format (specified by "region Settings" in "Control Panel)
& T the time format specified by "region Settings" in "Control Panel"
& T 24-hour time format
& P current page number
& P total number of pages
& B text align right (put the text to align right after "& B)
& B text Center (place the text to center between "& B" and "& B)
& Single & number (&)
Note: 1. These symbols can be combined with text. For example, "page number, & p/& P" in this tutorial ".
2. By default, the header and footer are left aligned. Therefore, IE only provides the setting symbol in the right aligned and footer.
3. We recommend that you set the header to be empty and the footer to "& B page & p page/total & P page & B ", the print effect is to display "1st pages/4 pages in total" in the center of the footer.
Once you know the above knowledge, you can print it. But sometimes you will find that some images on the Web page are lost during printing. Why? Why? Do not worry. The default settings of IE do not print the background color and image of the webpage. You only need to enter Internet Options of IE and select (check) it. The specific operation method is as follows: click "Tools"> "Internet Options" in the main menu of IE. In the displayed dialog box, click the "advanced" tab, find "print background color and image. As shown in:

Then, check the options circled in red and click "OK.

So far, we have completed the print settings for IE. Next, as long as we haven't disabled IE, we can always use the above settings (the last "print background color and image" Change is always valid) to print. Print and preview before printing. Print after the effect is satisfactory.

3. windows built-in functions
Press and hold the left mouse button, select the content you want to print, and right-click and select print ", in the displayed Print dialog box, select "selected range" in "page range" to print only the content you selected.

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.