Javascript content printing method summary _ javascript skills

Source: Internet
Author: User
About JS printing! Online collection + self-Summary. For more information, see. Window. print () is used for general printing.
However, this method is usually used for selective printing. Therefore, the following method is called:

The Code is as follows:


Function preview ()
{
Bdhtml###doc ument. body. innerHTML;
Sprnstr =" ";
Eprnstr =" ";
Prnhtml = bdhtml. substr (bdhtml. indexOf (sprnstr) + 17 );
Prnhtml = prnhtml. substring (0, prnhtml. indexOf (eprnstr ));
Too many Doc ument. body. innerHTML = prnhtml;
Window. print ();
}


The problem is coming again ~
There will be footer and header ~ There are also horizontal and vertical points ~
What should I do?
It is best to use print preview ~
Because it has settings
In this case, an IE control "WebBrowser" must be referenced"
Reference in the page:


Its control method:

The Code is as follows:


WebBrowser. ExecWB () Open
WebBrowser. ExecWB () Close all existing IE Windows and open a new window
WebBrowser. ExecWB () Save the webpage
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 () seems to be undo, to be confirmed
Select All WebBrowser. ExecWB (17,1)
WebBrowser. ExecWB (22, 1) Refresh
WebBrowser. ExecWB () Close form no prompt


Example:

Print Test

Copy and paste any text file ~ Change the suffix ~
Ie running ~ Click Preview ~ Set printing ~ Print ~ OK
And then test it in the project ~ For example, in aspx
Run the same job. Click Preview ~
Region ~ How can I see the ie Security Warning "internet explorer" has blocked this site from using ActiveX controls in unsafe ways"
At this time, we need to change the security settings of ie (if no patch is installed, it will be okay ~ That is your "rp", not necessarily the customer and other people's "rp" is as good as you)
Operation:
Main Menu "Tools" -- Internet Options -- Security -- Custom Level, initialize the ActiveX control that is not marked as secure in "Security Settings" and run the script from "disabled" to "enabled"
Another method is to use "Tools" "Internet Options" "Trusted Sites (Trusted Sites)" "sites" and enter the website address. If the website is not based on https: for the connection, remove the check box before "require server verification (https :) for all sites in the region.
(For LAN: "tool" "Internet option" "local Internet" "advanced ")
(The wireless network does not seem to be a LAN)
Recommendation 2 ~
Everything is ready ~ OK ~
Print the method set (not tested one by one ):

The Code is as follows:


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



New Document



Script
Var hkey_root, hkey_path, hkey_key
Hkey_root = "HKEY_CURRENT_USER"
Hkey_path = "file: // software // Microsoft // Internet Explorer \ PageSetup \\"
// Set the page header and footer to be empty.
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 and footer to the default value.
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 setphidden (id) {// hide all layers other than the specified id
Var ps = document. getElementsByTagName ("DIV ");
For (var I = 0; I {
If (ps. item (I). id! = Id)
Ps. item (I). style. display = "none ";
}
}
Function setpvisible (id) {// display all layers other than the specified id
Var ps = document. getElementsByTagName ("DIV ");
For (var I = 0; I {
If (ps. item (I). id! = Id)
Ps. item (I). style. display = "block ";
}
}
Function printpr () // preview a function
{
Pagesetup_null (); // remove the header and footer before previewing.
Setphidden ("p1"); // hide the elements that do not want to print the output before printing
Var WebBrowser =' ';
Document. body. insertAdjacentHTML ('beforeend', WebBrowser); // Add html to the body Tag (WebBrowser activeX Control)
WebBrowser1.ExecWB (7, 1); // print preview
WebBrowser1.outerHTML = ""; // clear the inserted html code from the code
Pagesetup_default (); // restores the default value of the header and footer After previewing.
Setpvisible ("p1"); // a button is displayed after the preview is complete.
}
Function print () // print the function
{
Pagesetup_null (); // remove the header and footer before printing
Setphidden ("p1"); // hide the elements that do not want to print the output before printing
Var WebBrowser =' ';
Document. body. insertAdjacentHTML ('beforeend', WebBrowser); // Add html to the body Tag (WebBrowser activeX Control)
WebBrowser1.ExecWB (6, 1); // print
WebBrowser1.outerHTML = ""; // clear the inserted html code from the code
Pagesetup_default (); // restores the default value of the header and footer after the print ends.
Setpvisible ("p1"); // display the button after printing
}
Script




Table 1:







1111 1111 1111
1111 1111 1111
1111 1111 1111





----------------------------------------------------------------------------
JS implements local printing and preview:
First:
JS to implement simple partial page Printing

The Code is as follows:


Function preview (preview)
{
If (then <10 ){
Bdhtml###doc ument. body. innerHTML; // obtain the html code of the current page
Sprnstr =" "; // Set the start area for printing.
Eprnstr =" "; // Set the print end Area
Prnhtml = bdhtml. substring (bdhtml. indexOf (sprnstr) + 18); // retrieves html from the start code
Prnhtml = prnhtml. substring (0, prnhtml. indexOf (eprnstr); // retrieves html from the end code
Too many Doc ument. body. innerHTML = prnhtml;
Window. print ();
Required parameter Doc ument. 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.