Home > Web Front-end > JS Tutorial > Summary of javascript printing content methods_javascript skills

Summary of javascript printing content methods_javascript skills

WBOY
Release: 2016-05-16 18:42:19
Original
1270 people have browsed it

Generally, for printing, just use window.print(); and it will be OK
But generally it is selective printing, so the method will be called:

Copy code The code is as follows:

function preview()
{
bdhtml=window.document.body.innerHTML;
sprnstr="";
prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr) 17);
prnhtml=prnhtml.substring( 0,prnhtml.indexOf(eprnstr));
window.document.body.innerHTML=prnhtml;
window.print();
}

The problem is here again~
There will be footers and headers~and horizontal and vertical ones~
What should I do?
The best thing is to use print preview~
Because it has settings
At this time, you must reference a control of IE "WebBrowser"
Quote in the page:


Its control method:
Copy code The code is as follows:

WebBrowser.ExecWB(1,1) Open
WebBrowser.ExecWB(2,1) Close now All IE windows and open a new window
WebBrowser.ExecWB(4,1) Save web page
WebBrowser.ExecWB(6,1) Print
WebBrowser.ExecWB(7,1) Print preview
WebBrowser.ExecWB(8,1) Print page settings
WebBrowser.ExecWB(10,1) View page properties
WebBrowser.ExecWB(15,1) It seems to be revoked and needs to be confirmed
WebBrowser.ExecWB( 17,1) Select all
WebBrowser.ExecWB(22,1) Refresh
WebBrowser.ExecWB(45,1) Close the form silently

Example:
< object id="WebBrowser" width="0" height="0" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2">
Print test

Copy and paste any text file~change the suffix name~
ie run~click preview~set printing~print~OK
Then test it into the project~ For example, in aspx
Run the same, click preview~
Hey~ Why does the IE security warning appear "Internet Explorer" has blocked this site from using ActiveX controls in an unsafe way"
At this time, you need to change the security settings of IE (if you haven’t installed the patch, it will be fine ~ that’s because your “rp” is good, not necessarily the “rp” of customers and other people is as good as yours)
Operation:
Main menu "Tools" - Internet Options - Security - Custom Level, change "Initialize and script run ActiveX controls not marked as safe" in "Security Settings" from "Disabled" to "Enabled"
Another method, "Tools" "Internet Options" "Trusted Sites (Trusted Sites)" "Site", then fill in the URL. If the website is not connected with https:, put the following " All sites in this area require server verification (https:). Just uncheck the box in front of it.
(If it is a LAN: "Tools" "Internet Options" "Local Internet" "Advanced")
(The wireless network does not seem to be a LAN)
Recommend the second option~
Everything is ready ~ok~
Print method set (not tested one by one):
Copy code The code is as follows:

-------------------------------------------------- -------------------------------



New Document



<script> <br>var hkey_root,hkey_path,hkey_key <br>hkey_root="HKEY_CURRENT_USER" <br>hkey_path="file://software//Microsoft//Internet Explorer\PageSetup\" <br>/ /Set the header and footer of web page printing to be empty <br>function pagesetup_null(){ <br>try{ <br>var RegWsh = new ActiveXObject("WScript.Shell") <br>hkey_key="header" <br> RegWsh.RegWrite(hkey_root hkey_path hkey_key,"") <br>hkey_key="footer" <br>RegWsh.RegWrite(hkey_root hkey_path hkey_key,"") <br>}catch(e){} <br>} <br> //Set the header and footer for web page printing to the default value <br>function pagesetup_default(){ <br>try{ <br>var RegWsh = new ActiveXObject("WScript.Shell") <br>hkey_key="header" <br>RegWsh.RegWrite(hkey_root hkey_path hkey_key,"&w&bpage number,&p/&P") <br>hkey_key="footer" <br>RegWsh.RegWrite(hkey_root hkey_path hkey_key,"&u&b&d") <br>}catch(e) {} <br>} <br>function setdivhidden(id){//Hide all layers except the specified id<br>var divs=document.getElementsByTagName("DIV"); <br>for(var i=0; i<divs.length;i ) <BR>{ <BR>if(divs.item(i).id!=id) <BR>divs.item(i).style.display="none"; <BR> } <BR>} <BR>function setdivvisible(id){//Display all layers other than the specified id<BR>var divs=document.getElementsByTagName("DIV"); <BR>for(var i=0;i&lt ;divs.length;i ) <BR>{ <BR>if(divs.item(i).id!=id) <BR>divs.item(i).style.display="block"; <BR>} <BR>} <BR>function printpr() //Preview function<BR>{ <BR>pagesetup_null();//Remove the header and footer before previewing<BR>setdivhidden("div1");//Before printing First hide the elements that you do not want to print out <BR>var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT> '; <br>document.body.insertAdjacentHTML('beforeEnd', WebBrowser);//Add html (WebBrowser activeX control) to the body tag <br>WebBrowser1.ExecWB(7, 1);//Print preview<br> WebBrowser1.outerHTML = "";//Clear the inserted html code from the code<br>pagesetup_default();//Restore the header and footer to the default value after the preview is finished<br>setdivvisible("div1");//The preview is finished Display button after <br>} <br>function print() //Print function <br>{ <br>pagesetup_null();//Remove the header and footer before printing <br>setdivhidden("div1"); / /Hide the elements you don’t want to print before printing <br>var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2">< /OBJECT>'; <br>document.body.insertAdjacentHTML('beforeEnd', WebBrowser);//Add html in the body tag (WebBrowser activeX control) <br>WebBrowser1.ExecWB(6, 1);//Print<br>WebBrowser1.outerHTML = "";//Clear the inserted html code from the code<br>pagesetup_default();//After printing, the header and footer return to their default values<br>setdivvisible("div1");// Show button after printing is finished <br>} <br></script>




Form 1:



;/td>
111111111111
111111111111
11111111




-------------------------------- -----------------------------------------------
JS implementation partial Print and preview:
First type:
JS implements simple page partial printing

Copy code The code is as follows:

function preview(oper)
{
if (oper < 10){
bdhtml=window.document.body.innerHTML;//Get the html code of the current page
sprnstr="";//Set the printing start area
eprnstr="";//Set the printing end Area
prnhtml=bdhtml.substring(bdhtml.indexOf(sprnstr) 18); //Get html from the start code backwards
prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr)); //From End the code and fetch html forward
window.document.body.innerHTML=prnhtml;
window.print();
window.document.body.innerHTML=bdhtml;
} else {
window.print();
}
}
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template