日期:2014-05-16  浏览次数:20529 次

Javascript 网页打印和打印预览

?

var w = screen.availWidth - 12;
var h = screen.availHeight - 75;
var urlStr = '#';
var subWin = window.open(urlStr,"newWin",'height='+h+',width='+w+', 
		top=0, left=0, toolbar=no, menubar=yes, scrollbars=yes, resizable=yes, status=no');
					
// subWin .print(); 打印
				
// 预览页面,只能支持IE
var OLECMDID = 7;
/* OLECMDID values:
* 6 - print
* 7 - print preview
* 1 - open window
* 4 - Save As
*/
				
var bd = null;
try {
	bd = newWin.document.body;
} catch(e) { }

while(!bd) {
	try {
		bd = newWin.document.body;
	} catch(e) { }
}
				
if(!bd) {}
else {
	var PROMPT = 1;  
	var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 '
			+ ' CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
	try {
		newWin.document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
		newWin.WebBrowser1.ExecWB(OLECMDID, PROMPT);
	} catch(e) {

	}
}