js打印功能

来源:互联网 发布:草木知春不久归全诗 编辑:程序博客网 时间:2024/05/21 09:58
1、window.print方式:
jsp页面 打印按钮:     
 <input type="button" value="打印" onclick="print();">
js 中:     
 function print(){
     window.print();
style样式中,设置隐藏按钮打印:
<style>  
     @media   print   {  
      .noprint{display:none}  
      }  
 </style>
2、WebBrowser控件方式
WebBrowser是IE内置的浏览器控件,无需用户下载.
一、WebBrowser控件
  <object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>
二、WebBrowder控件的方法
//打印
WebBrowser1.ExecWB(6,1);
//打印设置
WebBrowser1.ExecWB(8,1);
//打印预览
WebBrowser1.ExecWB(7,1);
关于这个组件还有其他的用法,列举如下:
WebBrowser.ExecWB(1,1) 打开
Web.ExecWB(2,1) 关闭现在所有的IE窗口,并打开一个新窗口
Web.ExecWB(4,1) 保存网页
Web.ExecWB(6,1) 打印
Web.ExecWB(7,1) 打印预览
Web.ExecWB(8,1) 打印页面设置
Web.ExecWB(10,1) 查看页面属性
Web.ExecWB(15,1) 好像是撤销,有待确认
Web.ExecWB(17,1) 全选
Web.ExecWB(22,1) 刷新
Web.ExecWB(45,1) 关闭窗体无提示
但是打印是会把整个页面都打印出来的,页面里面有什么东西就打印出来,我们有时候只需要打印数据表格,这时我们就要写一个样式了:把不想打印的部份隐藏起来:
样式内容:
<style type="text/css" media=print>
.noprint...{display : none }
</style>
然后使用样式就可以:
<p class="noprint">不需要打印的地方</p>
代码如下:
<script language="javascript">
function printsetup()...{
// 打印页面设置
wb.execwb(8,1);
function printpreview()...{
// 打印页面预览
wb.execwb(7,1);
function printit()
...{
if (confirm('确定打印吗?')) ...{
wb.execwb(6,6)
</script>
<OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height=0 id=wb name=wb width=0></OBJECT>
<input type=button name=button_print value="打印" class="noprint" onclick="javascript:printit()">
<input type=button name=button_setup value="打印页面设置" class="noprint" onclick="javascript:printsetup();">
<input type=button name=button_show value="打印预览" class="noprint" onclick="javascript:printpreview();">
3、去除页眉和页脚 <HTML><HEAD><TITLE>web打印去掉页眉页脚,以及不想打印出的页面元素</TITLE>   <META http-equiv=Content-Type content="text/html; charset=gb2312">   <SCRIPT language=javascript>   function printpr() //预览函数   {   document.all("qingkongyema").click();//打印之前去掉页眉,页脚   document.all("dayinDiv").style.display="none"; //打印之前先隐藏不想打印输出的元素(此例中隐藏“打印”和“打印预览”两个按钮)   var OLECMDID = 7;   var PROMPT = 1;   var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';   document.body.insertAdjacentHTML('beforeEnd', WebBrowser);   WebBrowser1.ExecWB(OLECMDID, PROMPT);   WebBrowser1.outerHTML = "";   document.all("dayinDiv").style.display="";//打印之后将该元素显示出来(显示出“打印”和“打印预览”两个按钮,方便别人下次打印)   }      function printTure() //打印函数   {   document.all('qingkongyema').click();//同上   document.all("dayinDiv").style.display="none";//同上   window.print();   document.all("dayinDiv").style.display="";   }   function doPage()   {   layLoading.style.display = "none";//同上   }            </SCRIPT>            <script language="VBScript">   dim hkey_root,hkey_path,hkey_key   hkey_root="HKEY_CURRENT_USER"   hkey_path="\Software\Microsoft\Internet Explorer\PageSetup"   '//设置网页打印的页眉页脚为空   function pagesetup_null()   on error resume next   Set RegWsh = CreateObject("WScript.Shell")   hkey_key="\header"   RegWsh.RegWrite hkey_root+hkey_path+hkey_key,""   hkey_key="\footer"   RegWsh.RegWrite hkey_root+hkey_path+hkey_key,""   end function   '//设置网页打印的页眉页脚为默认值   function pagesetup_default()   on error resume next   Set RegWsh = CreateObject("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"   end function   </script>            </HEAD>   <BODY background="images/background_01.gif" leftMargin=0   topMargin=0 rightMargin=0 bottomMargin=0 style="BACKGROUND-POSITION: center 50%">            <DIV align=center>      你希望打印的内容..........   </DIV>            <DIV align="center" id="dayinDiv" name="dayinDiv"><input type="button" class="tab" value="打印" onclick="printTure();">     <input   type="button" class="tab" value="打印预览" onclick="printpr();">   <input type="hidden" name="qingkongyema" id="qingkongyema" class="tab" value="清空页码" onclick="pagesetup_null()">     <input type="hidden" class="tab" value="恢复页码" onclick="pagesetup_default()">   </DIV>            </BODY>   </HTML>   更多问题欢迎来提问题网(shenzhidao.com)
0 0