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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM