JS simple method to print specified content on the page

Source: Internet
Author: User

Print the required content on the Web. Here is a simple setting:

<SCRIPT type = "text/JavaScript"> // print var hkey_root, hkey_path, hkey_key; hkey_root = "HKEY_CURRENT_USER "; hkey_path = "\ Software \ Microsoft \ Internet Explorer \ pagesetup \"; function doprint () {pagesetup_null (); // set the page header, footer, and page width. bdhtml?##doc ument. body. innerhtml; sprnstr = "<! -- Startprint --> "; // print start point eprnstr =" <! -- Endprint --> "; // print the destination prnhtml = bdhtml. substr (bdhtml. indexof (sprnstr) + 17); prnhtml = prnhtml. substring (0, prnhtml. indexof (eprnstr); Specify your doc ument. body. innerhtml = prnhtml; window. print (); // print pagesetup_default (); // restore the page header, footer, page width, and other information} // sets the function pagesetup_null () for printing the required page width, page header, and footer () {try {var regwsh = new activexobject ("wscript. shell "); hkey_key =" Header "; regwsh. regwrite (hkey_root + hkey_path + hkey_key, ""); hkey_key = "footer"; regwsh. regwrite (hkey_root + hkey_path + hkey_key, ""); hkey_key = "margin_bottom"; regwsh. regwrite (hkey_root + hkey_path + hkey_key, "0.200000"); hkey_key = "margin_left"; regwsh. regwrite (hkey_root + hkey_path + hkey_key, "0.200000"); hkey_key = "margin_right"; regwsh. regwrite (hkey_root + hkey_path + hkey_key, "0.200000"); hkey_key = "margin_top"; regwsh. regwrite (hkey_root + hkey_path + hkey_key, "0.200000");} catch (e ){}}
// Restore the default page width, header, and footer

Function pagesetup_default () {try {var regwsh = new activexobject ("wscript. shell "); hkey_key =" Header "; regwsh. regwrite (hkey_root + hkey_path + hkey_key, "W & B page number, & P/& P (& W)"); hkey_key = "footer"; regwsh. regwrite (hkey_root + hkey_path + hkey_key, "& U & B & D ");
Hkey_key = "margin_bottom"; regwsh. regwrite (hkey_root + hkey_path + hkey_key, "0.750000"); hkey_key = "margin_left"; regwsh. regwrite (hkey_root + hkey_path + hkey_key, "0.750000"); hkey_key = "margin_right"; regwsh. regwrite (hkey_root + hkey_path + hkey_key, "0.750000"); hkey_key = "margin_top ";
Regwsh. regwrite (hkey_root + hkey_path + hkey_key, "0.750000");} catch (e ){}}
</SCRIPT>

<HTML> <body> <div> it is not included in the print area, so you do not need to set it. </Div> <Object ID = "webbrowser" classid = "CLSID: 8856f961-340a-11d0-a96b-00c04fd705a2" Height = "0" width = "0" viewastext> </Object> <! -- Of course, this sentence is needed. --> <! -- Startprint --> <div> This is where printing is needed. </div> <Div class = "noprint"> class = "noprint" does not need to be printed, you can set the class attribute of any tag to remove unnecessary printing. <Input id = "btnprint" type = "button" value = "print" onclick = "javascript: doprint ();"/> </div> <! -- Endprint --> </body> 

In this way, you can simply print the required web content.

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.