關於js 連接打印機 去掉頁眉頁腳

在web程序中,遇到很多的打印的問題,其中自動去掉頁眉頁腳也挺重要的,省去了用戶的點擊流程,打印出想要的東西,整理了《打印預覽》《打印》《打印設置》,其中添加了去掉頁眉頁腳的功能

下面,上代碼:

  1. <HEAD>   
  2. <TITLE> New Document </TITLE>   
  3. <META NAME="Generator" CONTENT="">   
  4. <META NAME="Author" CONTENT="YC"> 
  5. <!-- 設置打印的區域-->   
  6. <style media="print">    
  7. .Noprint{display:none;}<!--用本樣式在打印時隱藏非打印項目-->   
  8. .PageNext{page-break-after: always;}<!--控制分頁--   
  9. </style>  
  10. <!-- 加載控件 -->  
  11. <OBJECT id="WebBrowser"  
  12.             classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0" width="0"></OBJECT>  
  13. <script language="JavaScript">  
  14. var hkey_root,hkey_path,hkey_key;  
  15. hkey_root = "HKEY_CURRENT_USER";  
  16. hkey_path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";  
  17. //設置網頁打印的頁眉頁腳爲空  
  18. function pagesetup_null(){  
  19.     var RegWsh = new ActiveXObject("WScript.Shell");  
  20.     hkey_key="header";  
  21.     RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");  
  22.     hkey_key="footer";  
  23.     RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"");  
  24. }  
  25. //設置網頁打印的頁眉頁腳爲默認值s  
  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. function PrintPage()  
  37. {  
  38. pagesetup_null();  
  39. document.all.WebBrowser.ExecWB(6,6);  
  40. pagesetup_default();  
  41. }  
  42.   
  43. function PrintPreview()  
  44. {  
  45. pagesetup_null();  
  46. document.all.WebBrowser.ExecWB(7,1);  
  47. pagesetup_default();  
  48. }  
  49.   
  50. function PrintSetup()  
  51. {  
  52. pagesetup_null();  
  53. document.all.WebBrowser.ExecWB(8,1);  
  54. pagesetup_default();  
  55. }  
  56. </script>  
  57.   
  58. </HEAD>   
  59.   
  60. <BODY>   
  61. <table>  
  62.     <tr>  
  63.         <td>1111</td>  
  64.         <td>1111</td>  
  65.     </tr>  
  66. </table>  
  67.   
  68. <table  class="Noprint">  
  69.       
  70.     <tr>  
  71.     <td><input type="button" value="打印" onclick=PrintPage()></td>  
  72.     <td><input type="button" value="打印預覽" onclick=PrintPreview()></td>  
  73.     <td><input type="button" value="打印設置" onclick=PrintSetup()></td>  
  74. </tr>  
  75. </p>   
  76. </BODY>   
  77. </HTML>   

點擊打印預覽的時候,效果去掉了頁眉和頁腳,完成。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章