JavaScript Print Content method Summary _javascript Tips

Source: Internet
Author: User
General printing on the use of Window.print (); it's OK.
But generally it is selective printing so the method is invoked:
Copy Code code as follows:

Function Preview ()
{
bdhtml=window.document.body.innerhtml;
Sprnstr= "<!--startprint-->";
Eprnstr= "<!--endprint-->";
Prnhtml=bdhtml.substr (Bdhtml.indexof (SPRNSTR) +17);
Prnhtml=prnhtml.substring (0,prnhtml.indexof (EPRNSTR));
window.document.body.innerhtml=prnhtml;
Window.print ();
}

The problem comes again ~
There will be footers and headers ~ and horizontal and vertical points ~
What to do?
It is best to use the print preview ~
Because it has a set inside
In this case, you must refer to IE's one control "WebBrowser"
Reference in the page:
<object id= "WebBrowser" width=0 height=0 classid= "CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" >
</object>
Its control methods:
Copy Code code as follows:

WEBBROWSER.EXECWB (1,1) Open
WEBBROWSER.EXECWB (2,1) closes all of the IE windows now and opens a new window
WEBBROWSER.EXECWB (4,1) Save Web page
WEBBROWSER.EXECWB (6,1) printing
WEBBROWSER.EXECWB (7,1) Print preview
WEBBROWSER.EXECWB (8,1) print Page Setup
WEBBROWSER.EXECWB (10,1) view page Properties
WEBBROWSER.EXECWB (15,1) seems to be withdrawn, pending confirmation
WEBBROWSER.EXECWB (17,1) Select all
WEBBROWSER.EXECWB (22,1) Refresh
WEBBROWSER.EXECWB (45,1) Close the form without prompting

Example:
<object id= "WebBrowser" width= "0" height= "0" classid= "CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" >
Print test
<input type= "button" onclick= "WEBBROWSER.EXECWB (7,1)" value= "Print Preview" >
Copy and paste with a text file ~ Change suffix name ~
IE Run ~ Point Preview ~ Set Print ~ Print ~ok
And then take it to the project, like ASPX.
Also run, point preview ~
How do you appear? IE Security Warning "Internet Explorer" has blocked this site from using ActiveX controls in unsafe ways
At this point, we will change the security settings ie (if you do not install the patch will be fine ~ that is You "RP" good, not necessarily customers and other people's "RP" and you as Good)
Operation:
Main Menu Tools--internet option--security--Custom level, initialize "ActiveX controls not marked as safe" in security settings, and script run from "Disable" to "Enable"
Another way, "Tools Internet Options Trusted sites (Trusted sites) site, and then fill in the URL, if the site is not https: connected to the" all sites in the zone Require server verification (https:) "To remove the check.
(if it is a local area network: "Tools" "Internet Options" "Local Internet" "Advanced")
(Wireless network does not seem to be LAN)
Recommend the second kind of ~
Everything is ready ~ok ~
Print method Set (no one by one tests):
Copy Code code as follows:

--------------------------------------------------------------------------
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<meta name= "generator" content= "EditPlus" >
<meta name= "Author" content= "" >
<meta name= "Keywords" content= "" >
<meta name= "Description" content= "" ><script>
var Hkey_root,hkey_path,hkey_key
Hkey_root= "HKEY_CURRENT_USER"
Hkey_path= "File://software//Microsoft//Internet explorer\\pagesetup\\"
Set page header to print with empty headers and footers
function Pagesetup_null () {
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, "")
}catch (e) {}
}
Set the page header footer to be printed by default
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")
hkey_key= "Footer"
Regwsh.regwrite (Hkey_root+hkey_path+hkey_key, "&u&b&d")
}catch (e) {}
}
function Setdivhidden (ID) {//Hide all layers except the specified ID
var divs=document.getelementsbytagname ("DIV");
for (Var i=0;i<divs.length;i++)
{
if (Divs.item (i). Id!=id)
Divs.item (i). style.display= "None";
}
}
function setdivvisible (ID) {//display all layers except the specified ID
var divs=document.getelementsbytagname ("DIV");
for (Var i=0;i<divs.length;i++)
{
if (Divs.item (i). Id!=id)
Divs.item (i) style.display= "block";
}
}
function PRINTPR ()//preview functions
{
Pagesetup_null ()//before previewing, remove headers, footers
Setdivhidden ("Div1"); Hide elements that you do not want to print out before printing
var WebBrowser = ' <object id= ' WebBrowser1 ' width=0 height=0 classid= ' CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 ' ></OBJECT> ';
Document.body.insertAdjacentHTML (' BeforeEnd ', WebBrowser);//Add HTML to the body tag (WebBrowser ActiveX control)
WEBBROWSER1.EXECWB (7, 1);//Print Preview
webbrowser1.outerhtml = "";//clear inserted HTML code from code
Pagesetup_default ()///Header footer Restore defaults after end of preview
Setdivvisible ("Div1"),//Show button when preview is complete
}
function print ()//print functions
{
Pagesetup_null ()//Remove headers, footers before printing
Setdivhidden ("Div1"); Hide elements that you do not want to print before printing
var WebBrowser = ' <object id= ' WebBrowser1 ' width=0 height=0 classid= ' CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 ' ></OBJECT> ';
Document.body.insertAdjacentHTML (' BeforeEnd ', WebBrowser);//Add HTML to the body tag (WebBrowser ActiveX control)
WEBBROWSER1.EXECWB (6, 1);//Print
webbrowser1.outerhtml = "";//clear inserted HTML code from code
Pagesetup_default ()//Header footer Restore defaults after print finish
Setdivvisible ("Div1"),//Display button after printing ends
}
</script>
<body>
<div id=div0>
<input Type=button value= Print Preview onclick= "PRINTPR ()" >
<input Type=button onclick= "print ()" value= "Printing" >
Form I:
</div>
<div id=div1>
<table>
<tr><td>1111</td><td>1111</td><td>1111</td></tr>
<tr><td>1111</td><td>1111</td><td>1111</td></tr>
<tr><td>1111</td><td>1111</td><td>1111</td></tr>
</table>
</div>
</body>
</HTML>

----------------------------------------------------------------------------
JS to achieve local printing and preview:
First type:
JS to achieve a simple page local printing
Copy Code code as follows:

Function Preview (OPER)
{
if (Oper < 10) {
bdhtml=window.document.body.innerhtml;//get the HTML code for the current page
Sprnstr= "<!--startprint" +oper+ "-->";/set the print start area
Eprnstr= "<!--endprint" +oper+ "-->";//Set Print End area
Prnhtml=bdhtml.substring (Bdhtml.indexof (SPRNSTR) +18); Fetching HTML backwards from the start code
Prnhtml=prnhtml.substring (0,prnhtml.indexof (EPRNSTR));//forward HTML from end code
window.document.body.innerhtml=prnhtml;
Window.print ();
window.document.body.innerhtml=bdhtml;
} else {
Window.print ();
}
}
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.