您的位置:首页 > Web前端 > JavaScript

[原创]JavaScript的打印技术体会

2005-01-31 20:19 351 查看

特定内容的部分打印技术


1.打印某个frame的内容(frameB)
<html>
<frameset rows="20%,*">
<frame SRC="framea.htm" name="FrameA" noresize>
<frame SRC="frameb.htm" name="FrameB" noresize>
<noframes>
<body>
</body>
</noframes>
</frameset>
</html>

frameA中添加
<input type="button" value="Print the other frame" onclick="parent.frameB.print();">

2.打印某个对象中的内容,如textbox中的内容
frameA:
<html><head>
</head>
<body>
<form name="formB" action="javascript:window.print()">
<input type="text" name="textboxb" size="12" value="Print Me">
</form>
</body></html>
frameB:
<html><head>
<script>
function printotherframe()
{
parent.FrameB.formB.textboxb.focus()
parent.FrameB.formB.submit()
}
</script>
</head><body>
<form name="formA"><input type="button" value="Print the other frame" onclick="printotherframe()">
</body></html>

3.打印非当前页
技术同上,只是在frame显示中动一点点骗人的手脚
<frameset rows="100%,*">
<frame src="controlpage.html">
<frame src="pagetoprint.html">
</frameset>

<script language="JavaScript"><!--
if (window.print)
document.write('<form><input type="button" value="Print" onClick="parent.frames[1].focus();parent.frames[1].print()"><//form>');
//--></script>

4.统一页眉页脚
<script language="Javascript"><!--
function doprint() {
//save existing user's info
var h = factory.printing.header;
var f = factory.printing.footer;
//hide the button
document.all("printbtn").style.visibility = 'hidden';
//set header and footer to blank
  // -- advanced features
  factory.printing.SetMarginMeasure(2) // measure margins in inches
  factory.SetPageRange(false, 1, 3) // need pages from 1 to 3
  factory.printing.printer = "HP DeskJet 870C"
  factory.printing.copies = 2
  factory.printing.collate = true
  factory.printing.paperSize = "A4"
  factory.printing.paperSource = "Manual feed" //付费才能用。

factory.printing.header = "";
factory.printing.footer = "";
factory.printing.portrait=true; // factory.printing.portrait = false; 横,纵纸张设置
//print page without prompt
factory.DoPrint(false);
//restore user's info
factory.printing.header = h;
factory.printing.footer = f;
//show the print button
document.all("printbtn").style.visibility = 'visible';
}
//--></script>
<body>
<br><br><br>
<object id=factory style="display:none" classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" viewastext codebase="ScriptX.cab#Version=5,0,4,185">
</object>
<br><br>
<div id="printbtn"><input name=idPrint type=button value="Print the letter" onclick="doprint()"></div>
</body>

5.打开新窗口并自动打印
<object id=WBControl width=0 height=0 classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2></object>
<script language=VBScript><!-- //
Sub VBPrint() On Error Resume Next
WBControl.ExecWB 6,1 '7,2在不少文章中都有提到,这里就不多说了。
End Sub
//--></script>

<script language=JavaScript><!-- //
if (window.print) self.print();
else if (navigator.appName.indexOf('Microsoft') !=-1) VBPrint()
setTimeout('self.close()',3000); '看代码相信大家都能明白。如果有不明白的欢迎留言
//--></script>
html中
<form>
<input type="button" value="Print Price List" onClick="window.open('pricelist.html','newwin');">
</form>

//设置在body onload中时,系统将自动恢复原来的打印设置
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: