关于用js打印网页

发布时间:2009年05月19日      浏览次数:1003 次
一、基本打印功能
<script language="javascript">
      function printsetup(){
            // 打印页面设置
            wb.execwb(8,1);
      }
      function printpreview(){
            // 打印页面预览     
            wb.execwb(7,1);           
      }
      function printit(){
            if (confirm('确定打印吗?')) {
                  wb.execwb(60,60)
            }
      }
</script>
<input type=button name=button_print value="打印" onClick="javascript:printit()"/>
<input type=button name=button_setup value="打印页面设置" onClick="javascript:printsetup();"/>
<input type=button name=button_show value="打印预览" onClick="javascript:printpreview();"/>
<input type=button name=button_fh value="关闭" onClick="javascript:window.close();"/>
二、在打印的页面中加入分页符 1、先在页面中加入样式表
<style>
      .PageNext{page-break-after: always;}<!--控制分页-->
      .style8 {font-size: medium; color: #000066; }
</style>
2、在body中可以这样写
<table>
<tr>
<td><span class="style8">第一页</span></td>
</tr>
</table>
<!-- 下面这个网页元素就是用于打印时分隔页面-->
<div id="pageNext1" style="visibility:hidden;">
<p class="PageNext"></p>
</div>
<table>
<tr>
<td align="center"><span class="style8">第二页</span></td>
</tr>
</table>
三、在打印页面中去除不需要的元素 1、先在页面中加入这个样式
<style media=print>
      .Noprint{display:none;}<!--用本样式在打印时隐藏非打印项目-->
</style>
2、页面中不需要的元素可以这么写
<input id="abc" class="Noprint" type=button name=print value="打印本页面" onClick="javascript:window.print()">
<font class="Noprint">隐藏的文字</font> 四、设计打印页眉和页脚 1、在页面中加入js代码
<script language="JavaScript">
var hkey_root,hkey_path,hkey_key
hkey_root="HKEY_CURRENT_USER"
hkey_path="http://www.cnblogs.com/zhaoxinxin/admin/file://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 pagesetup_default(){
      try{
            var RegWsh = new ActiveXObject("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")
      }catch(e){}
}
</script>
2、在body中这么写
<input type="button" value="清空页码" onclick=pagesetup_null()>
<input type="button" value="恢复页码" onclick=pagesetup_default()>
<br/>
五、用样式表调节页面边距
<style media="print" type="text/css">
body {font-size:9pt; margin:-70px 0px 0px 0px }
</style>
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!