js 客户端打印html 并且去掉页眉、页脚

print() 方法用于打印当前窗口的内容,支持部分或者整个网页打印。

调用 print() 方法所引发的行为就像用户单击浏览器的打印按钮。通常,这会产生一个对话框,让用户可以取消或定制打印请求。

win10下测试ie11、chrome、firefox、360、edge 都可以成功去掉页眉页脚;

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <title>打印</title>
 5     <meta charset="utf-8">
 6     <style>
 7         .printBox {
 8             width: 300px;
 9             height: 300px;
10             border: 1px solid blue;
11         }
12     </style>
13     <!-- 打印的样式-->
14     <style media="print">
15         @page {
16             size: auto;
17             margin: 0mm;
18         }
19     </style>
20 </head>
21 
22 <body>
23 <div class="printBox">
24   this is content!!!<br>
25     点击按钮打印
26 </div>
27 <button οnclick='print_page()'>打印</button>
28 </body>
29 
30 <script type="text/javascript">
31     function print_page() {
32         if (!!window.ActiveXObject || "ActiveXObject" in window) { //是否ie
33             remove_ie_header_and_footer();
34         }
35         window.print();
36     }
37 
38     function remove_ie_header_and_footer() {
39         var hkey_path;
40         hkey_path = "HKEY_CURRENT_USER\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
41         try {
42             var RegWsh = new ActiveXObject("WScript.Shell");
43             RegWsh.RegWrite(hkey_path + "header", "");
44             RegWsh.RegWrite(hkey_path + "footer", "");
45         } catch (e) {
46         }
47     }
48 </script>
49 </html>

转载于:https://www.cnblogs.com/CandyManPing/p/7775083.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值