js呼叫列印當前頁的兩種方法
阿新 • • 發佈:2019-02-08
方法一:
<%@ 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>
方法二:
第二種方法需要外掛:smsx.cab<%@ 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>
這是外掛地址:http://download.csdn.net/detail/topwqp/4813265