javascript控制打印页面(转)

来源:互联网 发布:淘宝上的家具能买吗 编辑:程序博客网 时间:2024/04/29 20:20

 <HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="YC">
<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>
<br>
<br>
<br>
<br>
<br>
<br><p align=center>
<input type="button" value="清空页码" onclick=pagesetup_null()> <input type="button" value="恢复页吗" onclick=pagesetup_default()><br>

</p>
</BODY>
</HTML>

 

如何实现单击打印页面 (转)【自己学习】
解决思路
       因为打印是用得非常多的一个功能,所以实现的方法很多,下面就用三个方法来实现。
具体步骤
1.用window对象的print方法。

<button onClick="window.print()">打印</button>

2.用WebBrowser控件。

<object id="WebBrowser" width=0 height=0 
classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>
<button onClick="WebBrowser.ExecWB(8,1)">打印</button>

3.在文档区域执行print命令。

<button onClick="document.execCommand(’print’)">打印</button>

注意:第二种方法的控件需要IE5.5+的支持,而且因为使用了控件,如果客户端IE的安全性设置过高,将无法正常运行。
特别提示
运行本例代码,如果已经安装了打印机,将直接调用打印机打印页面,否则提示安装打印机,如图1.8.19所示。

图1.8.19 未安装打印机时的提示

特别说明


本例需要掌握的支持是如何在Web页中调用打印机打印该页。例中用了三种方法,一是window对象的print方法,二是WebBrowser控件的指定参数,三是print命令。至于WebBrowser控件和execCommand的其它参数,请参考第三部分问题24。
window.print() 打印与窗口关联的文档。