JavaScript Local page printing implements code _javascript skills

Source: Internet
Author: User
IE has an active control with it, but Firefox doesn't support it. Here the JS operation Dom method is used to print the text in the specified markup for the form, so you need to define the related label and its style (text size, font, and so on) when you use it.

Copy Code code as follows:

<script type= "Text/javascript" >
///***********************
Print the specified area page
Description: obj– through getElementById or other means to get the label ID, print the text in this obj
Date: 2009-8-7
function Startprint (obj)
{
var Owin=window.open ("", "_blank");
var strprint= "


Strprint=strprint + "<script type=\" text/javascript\ ">\n";
Strprint=strprint + "function Printwin () \ n";
Strprint=strprint + "{";
Strprint=strprint + "var owin=window.open" (\ "\", \ "_blank\"); \ n ";
Strprint=strprint + "OWin.document.write" (document.getElementById (\ "content\"). InnerHTML); \ n ";
Strprint=strprint + "owin.focus (); \ n";
Strprint=strprint + "oWin.document.close (); \ n";
Strprint=strprint + "Owin.print () \ n";
Strprint=strprint + "owin.close () \ n";
Strprint=strprint + "}\n";
Strprint=strprint + "<\/script>\n";

Strprint=strprint + "Strprint=strprint + "<div style= ' text-align:center ' ><button onclick= ' Printwin () ' style= ' padding-left:4px; padding-right:4px; ' > Print </button><button onclick= ' window.opener=null;window.close (); ' style= ' padding-left:4px; padding-right:4px; ' > Close </button></div>\n ";
OWin.document.write (Strprint);
Owin.focus ();
OWin.document.close ();
}
</script>

<button id= "Btnprint" onclick= "Startprint (document.getElementById (' content ')" > Print content </button>
<div id= "Content" >
<div style= "Font-size:12px;color: #333;" >
Here is the print content here is the print content here is the print content here is the printing contents here is the printing content here is the print content here is the print content here is the printing content here is the printing content here is the print content here is the print content here is the printing contents here is the printing content here is the print content here is the print content here is the print content here is the printing content here is the print content here is the print content here is the print content here is the print content here is the printing content here is the printing content here is the printing content here is the print content here is the printing content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content Print content here is the printed content here is the print content here is the print content here is the printing content here is the printing content here is the print content here is the print content here is the print content is print content here is the print content here is the print content here is the printing contents here is the print content here is the print content here is the print contents here is the printing content Here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the printing content here is the print content here is the print content here is the printing contents here is the print content here is the print content here is the print content here is the print content here is the print content this Inside is the print content here is the print content here is the print content here is the print content here is the print content
</div>
</div>


Here is the test code:
<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "> <ptml xmlns=" http://www.w3.org/1999/xhtml "> <pead> <meta http-equiv=" Content-type "content=" text/html; charset=gb2312 "/> <title> print designated Area page </title> <style type=" Text/css "> body{font-size:12px; Color: #333; } #content {border:solid 1px #333; Background: #ffc; } </style> <script type= "Text/javascript" >///***********************///Print the specified area page///description: obj--through Getelemen Tbyid or otherwise get the label identification, print the text in this obj///development: Wang Hongjian///Date: 2009-8-7 function Startprint (obj) {var owin=window.open ("", "_blank") ; var strprint= "<p > Print Preview Area </p>\n"; Strprint=strprint + "<script type=\" text/javascript\ ">\n"; Strprint=strprint + "function Printwin () \ n"; Strprint=strprint + "{"; Strprint=strprint + "var owin=window.open" (\ "\", \ "_blank\"); \ n "; Strprint=strprint + "OWIN.DOCUMENT.WRITE (document.getElementById (\ "content\"). InnerHTML); \ n "; Strprint=strprint + "owin.focus (); \ n"; Strprint=strprint + "oWin.document.close (); \ n"; Strprint=strprint + "Owin.print () \ n"; Strprint=strprint + "owin.close () \ n"; Strprint=strprint + "}\n"; Strprint=strprint + "<\/script>\n"; Strprint=strprint + "<pr size= ' 1 '/>\n"; Strprint=strprint + "<div id=\" content\ ">\n"; Strprint=strprint + obj.innerhtml + "\ n"; Strprint=strprint + "</div>\n"; Strprint=strprint + "<pr size= ' 1 '/>\n"; Strprint=strprint + "<div ><button onclick= ' Printwin () ' > Print </button><button onclick= ' Window.opener=null;window.close (); ' > Close </button></div>\n '; OWin.document.write (Strprint); Owin.focus (); OWin.document.close (); } </script> </pead> <body> <button id= "Btnprint onclick=" Startprint (' C Ontent ') "> Print content </button> <div id= "Content" > <div > here is the print content here is the print content here is the printing content here is the print content here is the printing content here is the print content here is the print contents here is the print content here is the print content Print content here is the printed content here is the print content here is the print content here is the printing content here is the printing content here is the print content here is the print content here is the print content is print content here is the print content here is the print content here is the printing contents here is the print content here is the print content here is the print contents here is the printing content Here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the printing content here is the print content here is the print content here is the printing contents here is the print content here is the print content here is the print content here is the print content here is the print content this Inside is the print content here is the printed content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the print content here is the printing contents here is the printing content here is the printing content here is the printing content here is the print content Print content here is the print content here is the print content here is the printing contents here is the print content here is the print content here is the print content here is the print content here is the printing content here is the printing content here is the print content here is the content of the printed contents </div> </div> </ Body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
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.