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              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 onclick='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/longbensong/p/7775083.html