zoukankan      html  css  js  c++  java
  • 分享js打印 网页

    <script type="text/javascript">
      var hkey_root, hkey_path, hkey_key
      hkey_root = "HKEY_CURRENT_USER"
      hkey_path = "\Software\Microsoft\Internet Explorer\PageSetup\"
                //设置网页打印的页眉页脚为空
                function pagesetup_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 printForm(){
        pagesetup_null();   //调用函数,去掉页眉页脚
        window.print();        //打印
       }
      </script>



    /* js打印功能,使用chrome浏览器体验最佳
    参数str可以使用id或者html等,如:printWork("divid");printWork("<div>TEST</div>") */
    function printWork(str) {
        var orderhtml = "";
        if (document.getElementById(str)) { orderhtml = document.getElementById(str).outerHTML; }
        else orderhtml = str;
        /* 创建iframe */
        var headobj = document.getElementsByTagName("head").item(0); //提取head  
        printFrame = document.getElementById("lldxi_printRegionFrame_2012_0112");
        if (printFrame) { document.body.removeChild(printFrame); }
        printFrame = document.createElement("iframe");
        printFrame.setAttribute("src", "about:blank");
        printFrame.setAttribute("id", "lldxi_printRegionFrame_2012_0112");
        printFrame.setAttribute("marginheight", "0");
        printFrame.setAttribute("marginwidth", "0");
        printFrame.style.display = "none";
        document.body.appendChild(printFrame);
        if (window.ActiveXObject)//ie
        {
            var htmlobj = printFrame.contentWindow.document.createElement("html"); var bodyobj = printFrame.contentWindow.document.createElement("body");
            bodyobj.innerHTML = orderhtml; htmlobj.appendChild(headobj.cloneNode(true)); htmlobj.appendChild(bodyobj);
            printFrame.contentWindow.document.appendChild(htmlobj); printFrame.contentWindow.document.execCommand("Print", true);
        }
        else {
            var htmlstr = "<html>" + headobj.outerHTML + "<body>" + orderhtml + "<script type="text/javascript">window.print();</script></body>" + "</html>";
            printFrame.contentWindow.document.write(htmlstr);
        }
    }




  • 相关阅读:
    redis 笔记04 服务器、复制
    redis 笔记03 RDB 持久化、AOF持久化、事件、客户端
    redis 笔记02 对象、数据库
    反射复习笔记02
    攻防世界 — Web进阶题(第11
    机器学习-极大似然和对数几率回归(浅入)
    redis的过期策略和内存淘汰
    Backbone.Router实践
    Spring Boot 数据访问
    Archives: 2018/12
  • 原文地址:https://www.cnblogs.com/yanergui/p/5014293.html
Copyright © 2011-2022 走看看