js打印功能
一、window.print方式:
jsp頁面 打印按鈕:
<input type="button" value="打印" onclick="print();">
js 中:
function print(){
window.print();
style樣式中,設置隱藏按鈕打印:
<style>
@media print {
.noprint{display:none}
}
</style>
二、WebBrowser控件方式
WebBrowser是IE內置的瀏覽器控件,無需用戶
下載
.
1、WebBrowser控件
<object ID='WebBrowser' WIDTH=0 HEIGHT=0 CLASSID='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>
2、WebBrowder控件的方法
//打印
WebBrowser1.ExecWB(6,1);
//打印設置
WebBrowser1.ExecWB(8,1);
//打印預覽
WebBrowser1.ExecWB(7,1);
關於這個組件還有其餘的用法,列舉以下:
WebBrowser.ExecWB(1,1) 打開
Web.ExecWB(2,1) 關閉如今全部的IE窗口,並打開一個新窗口
Web.ExecWB(4,1) 保存網頁
Web.ExecWB(6,1) 打印
Web.ExecWB(7,1) 打印預覽
Web.ExecWB(8,1) 打印頁面設置
Web.ExecWB(10,1) 查看頁面屬性
Web.ExecWB(15,1) 好像是撤銷,有待確認
Web.ExecWB(17,1) 全選
Web.ExecWB(22,1) 刷新
Web.ExecWB(45,1) 關閉窗體無提示
可是打印是會把整個頁面都打印出來的,頁面裏面有什麼東西就打印出來,咱們有時候只須要打印數據表格,這時咱們就要寫一個樣式了:把不想打印的部份隱藏起來:
樣式內容:
<style type="text/css" media=print>
.noprint...{display : none }
</style>
而後使用樣式就能夠:
<p class="noprint">不須要打印的地方</p>
代碼以下:
<script language="javascript">
function printsetup()...{
// 打印頁面設置
wb.execwb(8,1);
function printpreview()...{
// 打印頁面預覽
wb.execwb(7,1);
function printit()
...{
if (confirm('肯定打印嗎?')) ...{
wb.execwb(6,6)
</script>
<OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height=0 id=wb name=wb width=0></OBJECT>
<input type=button name=button_print value="打印" class="noprint" onclick="javascript:printit()">
<input type=button name=button_setup value="打印頁面設置" class="noprint" onclick="javascript:printsetup();">
<input type=button name=button_show value="打印預覽" class="noprint" onclick="javascript:printpreview();">
三、去除頁眉和頁腳 <HTML><HEAD><TITLE>web打印去掉頁眉頁腳,以及不想打印出的頁面元素</TITLE> <META http-equiv=Content-Type content="text/
html
; charset=gb2312"> <SCRIPT language=javascript> function printpr() //預覽函數 { document.all("qingkongyema").click();//打印以前去掉頁眉,頁腳 document.all("dayinDiv").style.display="none"; //打印以前先隱藏不想打印輸出的元素(此例中隱藏「打印」和「打印預覽」兩個按鈕) var OLECMDID = 7; var PROMPT = 1; var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>'; document.body.insertAdjacentHTML('beforeEnd', WebBrowser); WebBrowser1.ExecWB(OLECMDID, PROMPT); WebBrowser1.outerHTML = ""; document.all("dayinDiv").style.display="";//打印以後將該元素顯示出來(顯示出「打印」和「打印預覽」兩個按鈕,方便別人下次打印) } function printTure() //打印函數 { document.all('qingkongyema').click();//同上 document.all("dayinDiv").style.display="none";//同上 window.print(); document.all("dayinDiv").style.display=""; } function doPage() { layLoading.style.display = "none";//同上 } </SCRIPT> <script language="VBScript"> dim hkey_root,hkey_path,hkey_key hkey_root="HKEY_CURRENT_USER" hkey_path="\Software\Microsoft\Internet Explorer\PageSetup" '//設置網頁打印的頁眉頁腳爲空 function pagesetup_null() on error resume next Set RegWsh = CreateObject("WScript.Shell") hkey_key="\header" RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"" hkey_key="\footer" RegWsh.RegWrite hkey_root+hkey_path+hkey_key,"" end function '//設置網頁打印的頁眉頁腳爲默認值 function pagesetup_default() on error resume next Set RegWsh = CreateObject("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" end function </script> </HEAD> <BODY background="images/background_01.gif" leftMargin=0 topMargin=0 rightMargin=0 bottomMargin=0 style="BACKGROUND-POSITION: center 50%"> <DIV align=center> 你但願打印的內容.......... </DIV> <DIV align="center" id="dayinDiv" name="dayinDiv"><input type="button" class="tab" value="打印" onclick="printTure();"> <input type="button" class="tab" value="打印預覽" onclick="printpr();"> <input type="hidden" name="qingkongyema" id="qingkongyema" class="tab" value="清空頁碼" onclick="pagesetup_null()"> <input type="hidden" class="tab" value="恢復頁碼" onclick="pagesetup_default()"> </DIV> </BODY> </HTML> 更多問題歡迎來提問題網(shenzhidao.com)
歡迎關注本站公眾號,獲取更多信息