关于JavaScript打印去掉页眉页脚

因为这个问题,Google和百度都查了个遍,网上主要解决方案都是这一个代码:

<script language="JavaScript">
var hkey_root,hkey_path,hkey_key;
hkey_root="HKEY_CURRENT_USER";
hkey_path="\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){ alert(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){ alert(e); }
}

function printMethod(){
   pagesetup_default();
  window.print();
}
</script>

可是我试了,并不生效,不知是我的工程代码有误,还是网上这段代码有问题。反正各种方式都试了,怎么JavaScript还有css控制,然并卵。

最后发现,web打印产生页眉页脚,是浏览器的设置,而不是我们工程代码的问题,所以只要设置浏览器关闭页眉页脚,就可以了!

附:浏览器取消页眉页脚设置

IE浏览器(IE6以后):工具(页面右上角)、打印、页面设置,在页面设置里取消勾选页眉页脚所有选项

Chrome:工具(右上角三个点)、打印,在打印页面单机“更多设置”,取消勾选‘页眉页脚’

原文地址:https://www.cnblogs.com/guodongdidi/p/5994208.html