本文介绍在JavaScript中通过ActiveXObject操作Windows注册表,修改Internet Explorer页面设置,从而在使用window.print()打印时去除页眉和页脚。该方法仅适用于支持ActiveX的旧版IE浏览器,涉及对HKEY_CURRENT_USER下PageSetup键值的读写,包括页眉、页脚及上下左右边距的设置。
JavaScript调用ActiveXObject修改IE注册表以隐藏打印页眉页脚
若需在打印时去除页眉和页脚,可在打印前执行以下代码:var HKEY_Root,HKEY_Path,HKEY_Key;
具体实现代码如下:
HKEY_Root="HKEY_CURRENT_USER";
HKEY_Path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
var head,foot,top,bottom,left,right;
var Wsh=new ActiveXObject("WScript.Shell");
HKEY_Key="header";
//设置页眉(为空) 根据你自己要设置的填入
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
HKEY_Key="footer";
//设置页脚(为空) 根据你自己要设置的填入
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"");
HKEY_Key="margin_bottom";
//设置下页边距(0) 根据你自己要设置的填入
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"0");
HKEY_Key="margin_left";
//设置左页边距(0) 根据你自己要设置的填入
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"1");
HKEY_Key="margin_right";
//设置右页边距(0)
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"0");
HKEY_Key="margin_top";
//设置上页边距(8)
Wsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,"1");