js打印小结

时间:2023-03-09 02:29:29
js打印小结
<script type="text/javascript"> 
//打印必备参数
var hkey_root,hkey_path,hkey_key;
hkey_root="HKEY_CURRENT_USER";
hkey_path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\"; //打印机设置
function toPageSetup(){
document.all.WebBrowser.ExecWB(8,1);//打印机设置
}
//打印预览
function toPreview(){
document.all.WebBrowser.ExecWB(7,1);
}
/*
* 功能:打印
* 参数:isShowSetup 是否显示打印设置
*/
function printPage(isShowSetup){
pagesetup_null();
window.print();
}
//配置网页打印的页眉页脚为空
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,"");
//&b 第&p页/共&P页 &b
}catch(e){}
} </script>

jsp中引入:

<OBJECT id=WebBrowser classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 width=0></OBJECT>

jsp中引入样式:

<style media="print">
.Noprint {
display: none //不打印的内容的class
} .PageNext {
page-break-after: always; //在元素后面插入分页
}
</style>

法二:直接全部引进去,做相关内容的替换(有提示!)调用方法即可

function isIE(){
if (!!window.ActiveXObject || "ActiveXObject" in window)
return true;
else
return false;
}
/**
* 打印指定页面
* @return
*/
function printPage() {
window.focus();
if(isIE()){
//$(".td03").css("border","0px");
var printPage=document.getElementById("divPage");//要打印div的Id
var oldStr=document.body.innerHTML;
var newStr=printPage.innerHTML;
document.body.innerHTML=newStr; pagesetup_null(); document.body.className += ' ext-ie';
document.execCommand('print', false, null); document.body.innerHTML=oldStr;
return false;
}else{
//$(".td03").css("border","0px");//去样式
var printPage=document.getElementById("divPage");//要打印div的Id
var oldStr=document.body.innerHTML;
var newStr=printPage.innerHTML;
document.body.innerHTML=newStr; pagesetup_null();//去页眉页脚
window.print(); document.body.innerHTML=oldStr;
return false;
} //打印相关参数
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,"");
//&b 第&p页/共&P页 &b
}catch(e){}
}