Window.print () Print part of the page

Source: Internet
Author: User
Window.print () is actually a program call for the browser to print the function menu. With the click of the Print Function menu, can not be accurate pagination, can not set paper type, the problem of playing more can not talk about, but, it is possible to let users do not go to the Point menu, directly click on a button in the page, or a link inside call it. In fact, many users are printing this way, but the most fatal disadvantage is that you cannot set the printing parameters, such as paper type, margin, select a printer, and so on.
It should be noted that this method provides a onbeforeprint and onafterprint of events before and after printing. You can edit some formatting before printing, specially sent to print, and then processed back after printing.
function Window.onbeforeprint ()
{
Hide something that doesn't need to be printed
}
function Window.onafterprint ()
{
Let go of hidden elements
}

By using these two methods, you can achieve partial printing of the page

When calling Window.print (), you can use CSS to control whether something in the page is displayed
<style> @media print{. noprint{Display:none}} </style>


HTML is as follows:
<table width= "757" height= "174" border= "0" align= "center" cellpadding= "0" cellspacing= "0" > <tr class= "noprint "> <td height=" align= "center" valign= "Top" > &L" T;/td> </tr>
</table>
At this time <tr class= "noprint" > is not printed TR

The media type associated with media is the CSS property media type, which is used to directly introduce the properties of the medium. The syntax format is as follows: @media screen | Print | Projection | Braille | Aural | TV | Handheld |     All parameter Description screen: refers to computer screens.     Print: An opaque medium used for a printer.     Projection: Refers to the item that is used for display.     Braille: A Braille system that refers to a printed matter with tactile effects.     Aural: Refers to the speech electron synthesizer.     TV: A type of media for television.     Handheld: Refers to a handheld display device. All: For all media.

To print the contents of the page can be achieved by using a style sheet, the answer to the upstairs although you can also achieve only the printing part of the content, but the premise is printed in the content of the tags you do, in some cases also not satisfactory, the landlord may wish to try the following methods:
<style type= ' text/css ' >
@media Print {
Input, textarea, select, A,. noprint {display:none;}
}
</style>
The above style sets all input controls, selects controls, hyperlinks and elements of the specified type do not appear when printed, that is, do not print, and if you want to not print the elements only add style class "noprint" can be, such as:
<body>
<div> put any content that needs to be printed here <span class= ' noprint ' > This content does not print. </span><span style= ' color:red; font-size:20px; ' > This is a large word. </span></div>
<div class= ' noprint ' > Description: This information will not be printed. </div>
</body>

This method is called when you print. ~

How to handle printing part of page information
function Doprint () {
bdhtml=window.document.body.innerhtml; Get all the HTML code inside the body tag
Sprnstr= "<!--startprint-->"; Declares a string that represents the starting position of the print
Eprnstr= "<!--endprint-->"; Marks the end position of the print
Prnhtml=bdhtml.substr (Bdhtml.indexof (SPRNSTR) +17); Find the printed starting string and add 17, because the <!--startprint--> is 17, so add 17 so that the label will not print when printed
Prnhtml=prnhtml.substring (0,prnhtml.indexof (EPRNSTR)); It's the same as the top.
window.document.body.innerhtml=prnhtml; Get the Body object through the Window object and set the contents of the current form to print between the start and end markers so that you can print out only the parts you need, and no other useless information will be available.
Window.print ();
There are two points to be explained: The program declares a print start string and an ending string, based on getting these two strings
To get what you need to print, so you should have these two strings in your HTML code, and the position is like this: I
To print all the information in a <table></table> on the current page, you should add the start string before the <table> tag
Add the end-marked string after the </table> end tag. Just like this <!--startprint--><table></table><!--endprint-->
Here you need to pay attention to a place:<!--startprint--> between this and <table> tags do not have a space to enter such a null character
Otherwise, the program will run a problem.


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.