window.print()以无页眉,页脚的形式,只打印指定区域内容
Code
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="list.aspx.cs" Inherits="list" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml">
6<head runat="server">
7 <title></title>
8 <script language="javascript">
9 var hkey_root, hkey_path, hkey_key;
10 hkey_root = "HKEY_CURRENT_USER";
11 hkey_path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
12
13 //网页打印时清空页眉页脚
14 function pagesetup_null() {
15 try {
16 var RegWsh = new ActiveXObject("WScript.Shell")
17 hkey_key = "header"
18 RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "")
19 hkey_key = "footer"
20 RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "")
21 } catch (e) {
22 }
23 }
24
25 //网页打印的时恢复页眉页脚为默认值
26 function pagesetup_default() {
27 try {
28 var RegWsh = new ActiveXObject("WScript.Shell")
29 hkey_key = "header"
30 RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "&w&b页码,&p/&P")
31 hkey_key = "footer"
32 RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "&u&b&d")
33 } catch (e) {
34 }
35 }
36
37 function doPrint(printDiv) {
38 try {
39 pagesetup_null();
40
41 newwin = window.open("", "newwin", "height=" + window.screen.height + ",width=" + window.screen.width + ",toolbar=no,scrollbars=auto,menubar=no");
42 newwin.document.body.innerHTML = document.getElementById(printDiv).innerHTML;
43 newwin.window.print();
44 newwin.window.close();
45 pagesetup_default();
46 } catch (e) { }
47 }
48 </script>
49</head>
50<body>
51 <form id="form1" runat="server">
52 <div>
53
54 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
55 <hr style="height:1px; width:100%; background-color:Red;" />
56 <div id="myDiv">
57 <asp:Image ID="Image1" runat="server" /><br />
58
59 </div>
60 <input type="button" id="bt" onclick="javascript:doPrint('myDiv')" value="打印" />
61 </div>
62 </form>
63</body>
64</html>
65
1<%@ Page Language="C#" AutoEventWireup="true" CodeFile="list.aspx.cs" Inherits="list" %>
2
3<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4
5<html xmlns="http://www.w3.org/1999/xhtml">
6<head runat="server">
7 <title></title>
8 <script language="javascript">
9 var hkey_root, hkey_path, hkey_key;
10 hkey_root = "HKEY_CURRENT_USER";
11 hkey_path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
12
13 //网页打印时清空页眉页脚
14 function pagesetup_null() {
15 try {
16 var RegWsh = new ActiveXObject("WScript.Shell")
17 hkey_key = "header"
18 RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "")
19 hkey_key = "footer"
20 RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "")
21 } catch (e) {
22 }
23 }
24
25 //网页打印的时恢复页眉页脚为默认值
26 function pagesetup_default() {
27 try {
28 var RegWsh = new ActiveXObject("WScript.Shell")
29 hkey_key = "header"
30 RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "&w&b页码,&p/&P")
31 hkey_key = "footer"
32 RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "&u&b&d")
33 } catch (e) {
34 }
35 }
36
37 function doPrint(printDiv) {
38 try {
39 pagesetup_null();
40
41 newwin = window.open("", "newwin", "height=" + window.screen.height + ",width=" + window.screen.width + ",toolbar=no,scrollbars=auto,menubar=no");
42 newwin.document.body.innerHTML = document.getElementById(printDiv).innerHTML;
43 newwin.window.print();
44 newwin.window.close();
45 pagesetup_default();
46 } catch (e) { }
47 }
48 </script>
49</head>
50<body>
51 <form id="form1" runat="server">
52 <div>
53
54 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
55 <hr style="height:1px; width:100%; background-color:Red;" />
56 <div id="myDiv">
57 <asp:Image ID="Image1" runat="server" /><br />
58
59 </div>
60 <input type="button" id="bt" onclick="javascript:doPrint('myDiv')" value="打印" />
61 </div>
62 </form>
63</body>
64</html>
65
只打印myDiv中的内容,但是在最后又about:blank 和日期的出现