在web程序中,遇到很多的打印的問題,其中自動去掉頁眉頁腳也挺重要的,省去了用戶的點擊流程,打印出想要的東西,整理了《打印預覽》《打印》《打印設置》,其中添加了去掉頁眉頁腳的功能
下面,上代碼:
- <HEAD>
- <TITLE> New Document </TITLE>
- <META NAME="Generator" CONTENT="">
- <META NAME="Author" CONTENT="YC">
- <!-- 設置打印的區域-->
- <style media="print">
- .Noprint{display:none;}<!--用本樣式在打印時隱藏非打印項目-->
- .PageNext{page-break-after: always;}<!--控制分頁--
- </style>
- <!-- 加載控件 -->
- <OBJECT id="WebBrowser"
- classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0" width="0"></OBJECT>
- <script language="JavaScript">
- var hkey_root,hkey_path,hkey_key;
- hkey_root = "HKEY_CURRENT_USER";
- hkey_path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
- //設置網頁打印的頁眉頁腳爲空
- function pagesetup_null(){
- 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,"");
- }
- //設置網頁打印的頁眉頁腳爲默認值s
- function pagesetup_default(){
- try{
- var RegWsh = new ActiveXObject("WScript.Shell")
- hkey_key="header"
- RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&w&b頁碼,&p/&P")
- hkey_key="footer"
- RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&u&b&d")
- }catch(e){}
- }
- function PrintPage()
- {
- pagesetup_null();
- document.all.WebBrowser.ExecWB(6,6);
- pagesetup_default();
- }
- function PrintPreview()
- {
- pagesetup_null();
- document.all.WebBrowser.ExecWB(7,1);
- pagesetup_default();
- }
- function PrintSetup()
- {
- pagesetup_null();
- document.all.WebBrowser.ExecWB(8,1);
- pagesetup_default();
- }
- </script>
- </HEAD>
- <BODY>
- <table>
- <tr>
- <td>1111</td>
- <td>1111</td>
- </tr>
- </table>
- <table class="Noprint">
- <tr>
- <td><input type="button" value="打印" onclick=PrintPage()></td>
- <td><input type="button" value="打印預覽" onclick=PrintPreview()></td>
- <td><input type="button" value="打印設置" onclick=PrintSetup()></td>
- </tr>
- </p>
- </BODY>
- </HTML>
點擊打印預覽的時候,效果去掉了頁眉和頁腳,完成。