在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>   

点击打印预览的时候,效果去掉了页眉和页脚,完成。