JavaScript implementation Click button directly print _javascript tips

Source: Internet
Author: User

Many websites have this function, when browsing to the bottom will have a printing button, click the Print button can complete the printing function, the function is very good, user-friendly, the code is very simple.

 
  

That is, just call the Window.print () function to print the current page.

But the above is not perfect, because some of the content on the Web page does not need to print, the following describes how to print the contents of the page specified.

The code is as follows:

<! DOCTYPE html>
 
 

Special Note: Print preview needs to copy the code to the native test, otherwise there will be an error.

The above code to achieve the printed Web page to specify the effect of the content, the following brief introduction to the implementation process.

I. Principle of realization:

In the JS code in the use of Document.body.innerHTML =newstr, dynamic original body content to replace the content to be printed, after printing, in the original content restore, the principle is so simple, you can see code comments.

Two. Code comments:

1.function Printdiv (PrintPage) {}, declares a function that controls printing, the argument is an object, and the contents of this object are to be printed.
2.var newstr = printpage.innerhtml; To get the content to be printed.
3.var oldstr = Document.body.innerHTML, the contents of the original body.
4. Document.body.innerHTML =newstr, replace the contents of the original body with the content that will be printed.
5.window.print (), start printing.
6.DOCUMENT.BODY.INNERHTML=OLDSTR, and then restore the contents of the original body.

Three. Related reading:

The 1.window.print () function can refer to the section of the print () method of the Window object.
2.onclick events can be found in JavaScript's onclick events section.

The above content is relatively simple, and have a separate code comments to help you learn JS implementation Click button on the printing function, I hope this article is helpful to everyone.

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.