日期:2014-05-16  浏览次数:20377 次

JS 控制页面打印内容

<html>
<head>
?? <title>打印相关</title>
?? <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
?? <!--media=print 这个属性在打印时有效 有些不想打印出来的分页打印的都可以应用这类样式进行控制 在非打印时是无效的(可从打印预览中看到效果)-->
?? <style media=print>
??? /* 应用这个样式的在打印时隐藏 */
??? .Noprint {
???? display: none;
??? }
???
??? /* 应用这个样式的,从那个标签结束开始另算一页,之后在遇到再起一页,以此类推 */
??? .PageNext {
???? page-break-after: always;
??? }
?? </style>
???
?? <!-- 这个是普通样式 -->
???
?? <script type="text/javascript">
??? var hkey_root,hkey_path,hkey_key???
??? hkey_root="HKEY_CURRENT_USER";
??? hkey_path="\\Software\\Microsoft\\Internet?Explorer\\PageSetup\\";
????? //这个是用来设置打印页眉页脚的,你可以设置为空或者其它
????? 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){
????? alert(e.description());
????? }
???? </script>
</head>

<body>
?? <center class="Noprint">
??? <p>
???? <OBJECT id=WebBrowser classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 width=0 ></OBJECT>
???? <input type=button value=打印 onclick=document.all.WebBrowser.ExecWB(6,1)>
???? <input type=button value=直接打印 onclick=document.all.WebBrowser.ExecWB(6,6) />
???? <input type=button value=页面设置 onclick=document.all.WebBrowser.ExecWB(8,1) />
???? <input type=button value=打印预览 onclick=document.all.WebBrowser.ExecWB(7,1) />
???? <br />
??? </p>
??? <hr align="center" width="90%" size="1" noshade />
?? </center>

?? <table width="90%" border="0" align="center" cellpadding="2" cellspacing="0" class="tabp">
??? <tr>
???? <td>?? 第1页?? </td>
??? </tr>?
??? <tr>
???? <td>
????? <table width="100%" border="0" cellspacing="0" cellpadding="0">
?????? <tr>
??????? <td width="50%" class="tdp">
???????? 对于这类打印功能的,有一个问题,就是权限问题,一般这种和操控客户端东西的代码会有安全提示,一般用户也未必会理解,
???????? 所以像此类方法为了让使用者更明白,还需要多加注意,可以设置浏览器安全性中的未标记安全的ActiveX选项为提示,看到提示后点击(是或者允许)之类的词。
???????? 当然最简单但不推荐的方法就是直接设置安全性最低……
??????? </td>
?????? </tr>
????? </table>
???? </td>
??? </tr>
?? </table>
</body>
</html>