Javascript:print ()

Source: Internet
Author: User
Tags blank page

1. Realize printing function
1) EXECWB ()
A script program is used to invoke the print command below the IE menu. The implementation code is as follows:
<script language= "JavaScript" >

function Printsetup () {//print page Setup

WB.EXECWB (8,1);

}

function PrintPreview () {//print page Preview

WB.EXECWB (7,1);

}

function PrintIt () {//Print

if (confirm) (' OK to print. ')) {

WB.EXECWB (6,1)

}

}

</script>

<object classid=clsid:8856f961-340a-11d0-a96b-00c04fd705a2 height=0 ID=WB width=0></object>

<input Type=button name=button_print value= "print" onclick= "Javascript:printit ()" >

<input Type=button name=button_setup value= "Page Setup" onclick= "Javascript:printsetup ();" >

<input Type=button name=button_show value= "Print Preview" onclick= "Javascript:printpreview ();" >

<input Type=button name=button_fh value= "Close" onclick= "javascript:window.close ();" >


This object is actually? Iewebbrowser set the Width and Height, we can not see the shape of the control in the interface, then we will be able to use this object to implement our paper selection and preview function.

There are other uses for this component, as listed below:

L WB. EXECWB (1,1) Open

L WB. EXECWB (2,1) closes all of the IE windows now and opens a new window

L WB. EXECWB (4,1) Save Web page

L WB. EXECWB (6,1) printing

L WB. EXECWB (7,1) Print preview

L WB. EXECWB (8,1) print Page Setup

L WB. EXECWB (10,1) view page Properties

L WB. EXECWB (15,1) seems to be withdrawn, pending confirmation

L WB. EXECWB (17,1) Select all

L WB. EXECWB (22,1) Refresh

L WB. EXECWB (45,1) Close the form without prompting

2) Document.execcommand ()
<a href= "#" Onclick=document.execcommand ("print") > Print </a>

3) Window.print ()
Use: Javascript:window. Print ()

Prints the contents of the window.

method source
Window

Implementation version
Navigator 4.0



Syntax: print ()

Parameters: None

2. Implement the Set Play function: Using CSS
To get rid of the button that triggers the Print command, you just add the following code:

<style media= "Print" >

#btn1 {Display:none}

#btn2 {Display:none}

#btn3 {Display:none}

#btn4 {Display:none}

</STYLE>

Then add IDs to the above four buttons: Btn1, Btn2, Btn3, Btn4.

<input Type=button name=button_print value= "print" onclick= "Javascript:print ()" id= "BTN1" >

<input Type=button name=button_setup value= "Page Setup" onclick= "Javascript:printsetup ();" id= "btn2" >

<input Type=button name=button_show value= "Print preview onclick=" Javascript:printpreview (); "id=" Btn3 ">

<input Type=button name=button_fh value= "Close onclick=" javascript:window.close (); "id=" Btn4 ">


3. Remove header footer: Use JavaScript
<script language= "JavaScript" >

var Hkey_root,hkey_path,hkey_key

Hkey_root= "HKEY_CURRENT_USER"

Hkey_path=//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) {}

}

<Script>

4. Paging problem: Using CSS
<style type= "Text/css" >

div.pageend {Page-break-before:always}

</style>

Then use the page where you want to force pagination:

<div class= "Pageend" >

Content of pending pages

</div>

If you are using Page-break-before, then the content of the div is on the next page,

If you are using Page-break-after, then the content of the div is on the previous page

Page-break-after: Retrieves or sets the page break that appears after an object

Syntax: Page-break-after:auto | Always | Avoid | Left | Right | Null

Parameters:

L Auto: If you need to insert a page break after the object

L Always: Always insert page break after object

L Avoid: Avoid inserting page breaks behind objects

L Left: Inserts a page break after the object until it reaches a blank page edge

L Right: Inserts a page break after the object until it reaches a blank right margin

L NULL: null value. IE5 to remove page break settings

Example: p {page-break-after:always;}

Page-break-before: Retrieves or sets the page break that appears before the object

Syntax: Page-break-before:auto | Always | Avoid | Left | Right | Null

Parameters:

L Auto: Insert a page break before the object

L Always: Always insert a page break before the object

L Avoid: Avoid inserting page breaks in front of objects

L Left: Inserts a page break in front of the object until it reaches a blank page edge

L Right: Inserts a page break before the object until it reaches a blank right margin

L NULL: null value. IE5 to remove page break settings

Example: p {page-break-after:always;}

5. Do not eject the Print dialog box: set EXECWB parameters
<object id= "WebBrowser1" width= "0" height= "0" classid= "CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" >

</object>

<script language= "JavaScript" >

function print () {

WEBBROWSER1.EXECWB (6,1);

}

</script>

Then add a push button to the page:

<input Type=button name=print value= "print" onclick= "print ()" >

In this way, when the press button is pressed, a pop-up window prompts you to change the second argument 1 to 6 if you do not prompt.
6. Print paging problem (PageBreak in CSS can be controlled)
PageBreak: Forces the page to be changed before and after the style control when printing.

Before: The page break that appears before the object is set. When set to always, the page delimiter is always inserted before the object. The corresponding CSS property is "Page-break-before".

After: The page delimiter that appears after the object is set. When set to always, the page delimiter is always inserted after the object. The corresponding CSS property is ">.

With Page-break-after
Page-break-after version: CSS2 Compatibility: ie4+ inheritance: None
Grammar:
Page-break-after:auto | Always | Avoid | Left | Right | Null
Take value:
Auto: If you need to insert a page break after the object
Always: Always insert a page break after the object
Avoid: not supported. Avoid inserting a page break after an object
Left: not supported. Insert a page break after the object until it reaches a blank left margin
Right: not supported. Insert a page break after the object until it reaches a blank right margin
Null: a blank string. Remove Page break settings

Description
Retrieves or sets the page delimiter that appears after the object.
This property is useful when printing a document. This property is not used for BR or HR objects.
If a conflict occurs between the value of this property and the Page-break-before property on an object that is already displayed by the browser, the maximum number of paged values is used.
Page delimiters are not allowed to appear inside the anchored object.
In IE6 and previous versions of browsers, the left and right values are equivalent to always.
This property is read-only for Currentstyle objects. is writable for other objects.
The corresponding script attribute is pagebreakafter.
Example:
p {page-break-after:always;}

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.