js调用打印当前页的两种方法

方法一: 

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>  
<%@ page contentType="text/html; charset=GBK"%>  
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>  
  
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
%>  
  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
<html>  
  <head>  
    <base href="<%=basePath%>">  
      
    <title></title>  
      
    <meta http-equiv="pragma" content="no-cache">  
    <meta http-equiv="cache-control" content="no-cache">  
    <meta http-equiv="expires" content="0">      
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  
    <meta http-equiv="description" content="This is my page">  
    <!--  
    <link rel="stylesheet" type="text/css" href="styles.css">  
    -->  
    <script type="text/javascript">  
    function dy(){  
        bdhtml=window.document.body.innerHTML;   
        sprnstr="<!--startprint-->";   
        eprnstr="<!--endprint-->";   
        prnhtml=bdhtml.substr(bdhtml.indexOf(sprnstr)+17);   
        prnhtml=prnhtml.substring(0,prnhtml.indexOf(eprnstr));   
        window.document.body.innerHTML=prnhtml;  
        document.getElementById("dis").style.display="none";  
        window.print();  
        document.getElementById("dis").style.display="block";  
    }  
    </script>  
  </head>  
   
  <body>  
    <!--startprint-->  
    <table width="80%" align="center">  
        <tr id="dis">  
            <td align="right">  
                <input type="button" onclick="dy()" value="打印"/>  
            </td>  
        </tr>          
      
    </table>  
    <!--endprint-->  
  </body>  
</html>

方法二:

<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>  
<%@page import="cn.edu.tsinghua.yjszs.bm.vo.Dyzkz" %>  
<%  
String path = request.getContextPath();  
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  
%>  
  
  
<!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>  
<object id="factory" style="display: none"  
            classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814"  
            codebase="././smsx/smsx.cab#Version=6,5,439,30" viewastext></object>  
  
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />  
<title>打印</title>  
</head>  
<script language="javascript">  
function dy(){  
     
   
    document.getElementById("disp").style.display="none";     
    factory.printing.portrait = false; //portrait是指打印方向,设置为true就是纵向,false就是横向。   
      
    factory.DoPrint(true); //设置为false,直接打印   
      
    document.getElementById("disp").style.display="block";  
  
}  
</script>  
<body>  
//这个隐藏的iframe用于向后台传入数据,传输数据的同时页面不进行跳转。  
<iframe name="hideIframe" width=0 height=0></iframe>  
<form action="bm.yz_bs_qkb.do" method=post target="hideIframe" id="subform" name="subform">  
<input type="hidden" name ="m" id="m"/>  
</form>  
  
  
<div style="width: 900px;height: 600px">  
<div id="disp" style="float:left; width:100%; height:10%">  
    <table>  
        <tr>  
            <td>  
                <input type="button" value="打印" onclick="dy()"/>  
            </td>  
        </tr>  
    </table>  
</div>

第二种方法需要插件:smsx.cab

这是插件地址:http://download.csdn.net/detail/topwqp/4813265


转载于:https://my.oschina.net/yongqingfan/blog/618864

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值