1. 程式人生 > >頁面區域性列印:Window.print()--無需外掛,相容IE

頁面區域性列印:Window.print()--無需外掛,相容IE

Window.print()很強大好不好 ,外掛啥的咱們不稀罕

區域性列印通過樣式和呼叫方法隱藏,詳情見註釋
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8'>
    <title></title>
</head>
<script>
    // 隱藏列印內的內容之方式2 按ctrl+P 列印時有效 
    function onbeforeprint() {
        //列印之前執行的方法:將一些不需要列印的隱藏
        //document.getElementById("btn").hide();
        alert("此文件即將列印!");
    }
    function onafterprint() {
        //列印之後執行的方法:放開隱藏的元素
        //document.getElementById("btn").show();
        alert("此文件現在正在列印!");
    }
</script>

<body onbeforeprint="onbeforeprint()" onafterprint="onafterprint()">

    <h3>0000000000000000000000000000000000</h3>
    <!--start-->
    <h3>1111111111111111111111111111111111</h3>
    <h3>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</h3>
    <h3>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</h3>
    <h3>1111111111111111111111111111111111</h3>
    <h3>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</h3>
    <h3>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</h3>
    <h3>1111111111111111111111111111111111</h3>
    <h3>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</h3>
    <h3>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</h3>
    <!-- 隱藏列印內的內容之方式1:親測style標籤放在head標籤中設定無效 -->
    <style media="print">
        .noprint {
            display: none;
        }
    </style>
    <div id="btn">
        <input type="button" class="noprint" value="列印" onClick="printPage()" />
    </div>

    <!--end-->
    <iframe id="printf" src="" width="0" height="0" frameborder="0"></iframe>
    </div>
    <script type="text/javascript">
        var hkey_root, hkey_path, hkey_key;
        hkey_root = "HKEY_CURRENT_USER";
        hkey_path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\"; //網頁列印時設定清空頁首頁尾     


        function setup_null() {
            try {
                var RegWsh = new ActiveXObject("WScript.Shell")
                hkey_key = "header"
                RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "")
                hkey_key = "footer"
                RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "")
            } catch (e) { }
        }

        function setup_default() { //網頁列印時設定頁首頁尾預設值        
            try {
                var RegWsh = new ActiveXObject("WScript.Shell")
                hkey_key = "header"
                RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "&w&b頁碼,&p/&P")
                hkey_key = "footer"
                RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "&u&b&d")
            } catch (e) { }
        }

        function doPrint(printDiv) {
            try {
                setup_null();
                newwin = window.open("", "newwin", "height=" + window.screen.height + ",width=" + window.screen.width +
                    ",toolbar=no,scrollbars=auto,menubar=no");
                newwin.document.body.innerHTML = document.getElementById(printDiv).innerHTML;
                newwin.window.print();
                newwin.window.close();
                setup_default();
            } catch (e) { }
        }

        function printPage() { //獲取當前頁的html程式碼 
            setup_null();
            bdhtml = window.document.body.innerHTML;
            sprnstr = "<!--start-->";
            eprnstr = "<!--end-->";
            printhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 12);//這個數字貌似沒有用
            printhtml = printhtml.substring(0, printhtml.indexOf(eprnstr));
            f = document.getElementById('printf');
            f.contentDocument.write(printhtml); //寫入到新的iframe視窗
            f.contentDocument.close();
            f.contentWindow.print(); //在新的iframe視窗呼叫瀏覽器印表機
        }

    </script>
</body>

</html>