Example of printing a webpage in a specified area using JavaScript

Source: Internet
Author: User

How to specify the div area on a JavaScript page: Use window. open () open a new page (window) in the browser, and use expose Doc ument. write () writes the content of the specified div area to the new window document, document. close () close the document and use window. print () calls the printer to print the current document.

JavaScript print function myPrint (obj ):
Copy codeThe Code is as follows:
Function myPrint (obj ){
// Open a new window newWindow
Var newWindow = window. open ("Print window", "_ blank ");
// The content of the div to be printed
Var docStr = obj. innerHTML;
// Print the content and write it to the newWindow document.
Newclientdoc ument. write (docStr );
// Close the document
Newmediaworkflow Doc ument. close ();
// Call the printer
NewWindow. print ();
// Close the newWindow page
NewWindow. close ();
}

Myprint () call method:
Copy codeThe Code is as follows: myPrint (document. getElementById ('printdidid '));

Instance code:
Copy codeThe Code is as follows: <script>
Function myPrint (obj ){
Var newWindow = window. open ("Print window", "_ blank ");
Var docStr = obj. innerHTML;
Newclientdoc ument. write (docStr );
Newmediaworkflow Doc ument. close ();
NewWindow. print ();
NewWindow. close ();
}
</Script>
<Div id = "print">
<Hr/>
Print the demo area. Click print to load the content in the new window!
<Hr/>
</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.