在开发的项目中用到了一些调用VBScript来实现win系统中打印的功能
注意:打开窗口必须为 openNewWin( url, 650, 650);
js 代码
- function openNewWin(url,winWid,winHig){
- var openParam = ""; //打开窗口的参数
- w_top = screen.Height; //屏幕高度
- w_left = screen.Width;//屏幕宽度
- //打开居中窗口的参数值
- openParam = "left=" + (w_left-winWid)/2 + ",top=" + (w_top-winHig)/2 + ",width="
- +winWid+",height="+winHig+",toolbar=no, menubar=no, scrollbars=yes, resizable=yes,location=no,status=no";
- window.open(url,'',openParam);
- }
下面是一个完整的例子
js 代码
- <script language=< span="">"javascript">
- function printpr() //预览函数
- {
- alert("如需打印表格和背景,请操作以下步骤:打开IE→工具→Internet选项→高级→中点击“还原默认设置”,之后在“打印”下的,“打
- 印背景颜色及图片”选项前打勾。");
- document.all("qingkongyema").click();//打印之前去掉页眉,页脚
- document.all("dayinDiv").style.display="none"; //打印之前先隐藏不想打印输出的元素(此例中隐藏“打印”和“打印预览”两个按钮)
- //document.all("closeid").style.display="none";
- var OLECMDID = 7;
- var PROMPT = 1;
- var WebBrowser = '<object id=< span="">"WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2">';
- document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
- WebBrowser1.ExecWB(OLECMDID, PROMPT);
- WebBrowser1.outerHTML = "";
- document.all("dayinDiv").style.display="";//打印之后将该元素显示出来(显示出“打印”和“打印预览”两个按钮,方便别人下次打印
- )
- document.all("closeid").style.display="";
- }
- function printTure() //打印函数
- {
- alert("如需打印表格和背景,请操作以下步骤:打开IE→工具→Internet选项→高级→中点击“还原默认设置”,之后在“打印”下的,
- “打印背景颜色及图片”选项前打勾。");
- document.all('qingkongyema').click();//同上
- document.all("dayinDiv").style.display="none";//同上
- document.all("closeid").style.display="none";
- window.print();
- document.all("dayinDiv").style.display="";
- //document.all("closeid").style.display="";
- }
- function doPage()
- {
- layLoading.style.display = "none";//同上
- }
- </script>
- <script language=< span="">"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>