JavaScript实现的页面打印

本文介绍了一种使用JavaScript实现的通用打印页面方法。该方法通过将当前页面的innerHTML内容导入到一个通用打印页面中,并利用前台ActiveX对象来完成打印功能。文章提供了具体的JavaScript代码示例及通用打印页面的HTML结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

JavaScript实现的一段打印页面的通用JS代码.具体原理就是将当前页面的innerHTML内容导入到通用打印页面中,然后调用前台ActiveX对象实现页面内容的打印.假设通用打印页面为网站根目录下的CommonPage/Print.aspx,JS代码如下:

var win=null,arr=null;
    function PrintPage(url, arrPara)
    {
        win = window.open(document.location.pathname.substr(0,document.location.pathname.substr(1).indexOf('/')+1) + "/CommonPage/Print.aspx", "打印", "top=0,left=0,width=1024,height=800,toolbar=no, menubar=no, scrollbars=yes, resizable=yes,location=no, status=no");
        arr = arrPara;
       
        window.setTimeout("CheckPrintData()",50);
        return win;
    }
    function CheckPrintData()
    {
        if(win.document.getElementById("Div_Data") == null)
        {
            window.setTimeout("CheckPrintData()",50);
            return;
        }
       
        if(document.all.form1 == null)
        {
            alert("页面没有名称为'form1'的Form元素,打印失败!");
            return;
        }
       
        win.document.getElementById("Div_Data").innerHTML=document.all.form1.innerHTML;
       
        for(var i=0; i<arr.length; i++)
        {
            if(win.document.getElementById(arr) != null)
            {
                win.document.getElementById(arr).style.display = "none";
            }
        }
       
        if((win.document.location.href.toUpperCase().indexOf("JS_MANAGE")!=-1 ||
           win.document.location.href.toUpperCase().indexOf("HS_MANAGE")!=-1 ||
           win.document.location.href.toUpperCase().indexOf("ZL_MANAGE")!=-1) &&
           win.document.all.xls!=null)
        {
            for(var i=0;i<document.all.xls.rows.length;i++)
            {
                win.document.all.xls.rows(i).deleteCell(0);
            }
        }       
           
        win.document.all.MyWebBrowser.ExecWB(7,1);
        win.window.close();
    }

    通用打印页面(Print.aspx)的代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Print.aspx.cs" Inherits="Print" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>通用打印页面</title>
        <script type="text/javascript" language="javascript">
        var showNum = 0;
        function Check()
        {
            if(showNum++ > 0)
            {
                close();
            }
        }
        </script>
    </head>
    <body onfocus="Check();">
        <form id="form1" runat="server">
            <object id="MyWebBrowser" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0"
                width="0">
            </object>
            <div id="Div_Data">
                <br />
                <br />
                <br />
                &nbsp; &nbsp; &nbsp; 页面加载中,请稍候...<br />
                &nbsp;</div>
        </form>
    </body>
    </html>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值