JavaScript print pages Specify areas for example _javascript tips

Source: Internet
Author: User

JavaScript print page Specifies the div area principle: Use window.open () to open a new page (window) in the browser, and use Window.document.write () to write the contents of the specified Div area to the new window document. Document.close () Closes the document and uses Window.print () to call the printer to print the current document.

JavaScript print function Myprint (obj):

Copy Code code as follows:

function Myprint (obj) {
Open a new Window NewWindow
var newwindow=window.open ("Print Window", "_blank");
The contents of the DIV to print
var docstr = obj.innerhtml;
Print content write to NewWindow document
NewWindow.document.write (DOCSTR);
Close Document
NewWindow.document.close ();
Calling a printer
Newwindow.print ();
Close NewWindow Page
Newwindow.close ();
}

Myprint () Call method:

Copy Code code as follows:
Myprint (document.getElementById (' Printdivid '));

Instance code:

Copy Code code as follows:
<script>
function Myprint (obj) {
var newwindow=window.open ("Print Window", "_blank");
var docstr = obj.innerhtml;
NewWindow.document.write (DOCSTR);
NewWindow.document.close ();
Newwindow.print ();
Newwindow.close ();
}
</script>
<div id= "Print" >
Print the demo area, click on the print will load the contents of the new window!
</div>
<button onclick= "Myprint (document.getElementById (' print ')" > Print </button>

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.