pagesetup_null();
if (domObj) {
var allHtml = window.document.body.innerHTML;
var printHtml = domObj.outerHTML;
window.document.body.innerHTML = printHtml;
window.print();
window.document.body.innerHTML = allHtml;
} else {
window.print();
}
pagesetup_default();
}
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, "");
hkey_key = "margin_left";
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.0");
hkey_key = "margin_right";
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.0");
hkey_key = "margin_top";
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.0");
hkey_key = "margin_bottom";
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.0");
} catch (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");
hkey_key = "margin_left";
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.750000");
hkey_key = "margin_right";
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.750000");
hkey_key = "margin_top";
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.750000");
hkey_key = "margin_bottom";
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0.750000");
} catch (e) {}
}
-------------------------------------
jsp:示例
<body>
<input id="dayinDiv" name="dayinDiv" type="button" class="tab" value="打印" οnclick="printTure();">
<div id="printDiv">
--- 打印内容 ----
</div>
<script type="text/javascript">
/** *传入的IDS为需要屏蔽的元素id数组 */
/* function print(IDS) { //将需要屏蔽的元素隐藏
for (var i = 0; i < IDS.length; i++) {
var target = document.getElementById(IDS[i]);
target.style.display = "none";
}
var imagesarray = window.document.images
for (var i = 0; i < imagesarray.length; i++) {
imagesarray[i].style.display = "none";
}
//打印
window.print();
//显示屏蔽的元素
for (var i = 0; i < IDS.length; i++) {
var target = document.getElementById(IDS[i]);
target.style.display = "block";
}
for (var i = 0; i < imagesarray.length; i++) {
imagesarray[i].style.display = "block";
}
} */
function printTure() {
printPage(document.getElementById("printDiv"));
}
</script>
</body>