Javascript呼叫IE列印以及預覽列印的方法

七月 31, 2008 at 12:46 下午 | 張貼於Javascript/HTML | 26 則迴響
標籤: , , , , , ,

剛好有機會用到,所以我整理了Javascript關於IE列印的一些寫法,一般比較少見的,就是區塊列印以及區塊預覽列印,可能很多人都不曉得要怎麼處 理,以下是我查了一些語法資料,然後自己寫個sample測試出來的code,我的寫法不一定是最好的,如果大家如果有發現更好的寫法,希望可以互相交流 一下。

整頁列印:
html部分:
<INPUT TYPE="button" value="整頁列印" onclick="print()">

Javascript部分:
N/A

部分列印(只列印div包起來的網頁):
html部分:
<div id="block">
<P><img id=
"ruten" name="ruten" SRC="http://www.ruten.com.tw/imgs/2008/logo.gif"></P>
</div>
<input type=
"button" value="部分列印" onclick="printScreen(block)">

Javascript部分:
//列印div包起來的部分並且列印完畢後自動關閉列印網頁
function printScreen(block){
var value = block.innerHTML;
var printPage = window.open(
"","printPage","");
printPage.document.open();
printPage.document.write(
"<HTML><head></head><BODY onload='window.print();window.close()'>");
printPage.document.write(
"<PRE>");
printPage.document.write(value);
printPage.document.write(
"</PRE>");
printPage.document.close(
"</BODY></HTML>");
}

整頁預覽列印:
html部分:
<OBJECT classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height=0 id=wb name=wb width=0></OBJECT>
<INPUT TYPE=
"button" value="整頁預覽" onclick="javascript:wb.execwb(7,1)">

Javascript部分:
N/A

部分預覽列印(只預覽div包起來的部份):
html部分:
<div id="block">
<P><img id=
"ruten" name="ruten" SRC="http://www.ruten.com.tw/imgs/2008/logo.gif"></P></div>
<input type=
"button" value="區塊預覽" onclick="previewScreen(block)">

Javascript部分:
//預覽div包起來的部分並且列印完畢後自動關閉列印網頁
function previewScreen(block){
var value = block.innerHTML;
var printPage = window.open(
"","printPage","");
printPage.document.open();
printPage.document.write(
"<OBJECT classid='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2' height=0 id=wc name=wc width=0></OBJECT>");
printPage.document.write(
"<HTML><head></head><BODY onload='javascript:wc.execwb(7,1);window.close()'>");
printPage.document.write(
"<PRE>");
printPage.document.write(value);
printPage.document.write(
"</PRE>");
printPage.document.close(
"</BODY></HTML>");
}

廣告

26 則迴響 »

RSS feed for comments on this post. TrackBack URI

  1. 請問板大這樣的語法是否目前電腦的安全性
    已預設不能使用了
    我找了很多類似的語法測試
    好像都是
    的安全性問題
    是可以在主機設定為安全嗎?
    我的是XP SP3 IE6
    都會跳出 訊息 這是不安全的ActionX
    然後按按鈕就跳出不支援此屬性或方法

    • 您好,Javascript是在client端執行的,所以您不能執行的問題是不會跟server有關係,您可以檢查看看把browser的近端網路安全性調低試試看,或是到進階裡面把ActiveX的選項都打開, 另外也可以改用firefox來測試看看,我這裡是都可以執行。

  2. 謝謝版大的說明
    小弟是想先讓使用者按按鈕可以預覽列印,我知道將AciveX及安性降低都可以正常跳出預覽頁面,只是出現不能正常使用時,要叫瀏覽者再去改電腦的設定,使用者一定會覺得麻煩,有的也不見得會改設定,想說是不是有什麼方法可以更有效的避免這種情況發生。
    另小弟請問板大在FireFox也是能夠使用以上語法預覽列印嗎?
    wb.execwb(7,1)不是只有IE才能辨別?
    或是您是指print()函式
    謝謝

    • 您好, 我是Win2000+IE6, 用預設設定是都可以執行的, 晚點有空再幫你測XP+IE7, 另外預覽是只有IE可以用沒錯。

    • 您好,我測了一下IE7預設是會擋掉ActiveX,必須再另外開啟, 至於ActiveX明明就是for IE only, 卻在IE7預設就把它擋掉, 真的是很怪的作法, 只能說微軟ActiveX的設計就很有問題。

      我後來google了一下,似乎沒有什麼比較有效的方法, 但是找到有個似乎可以模擬它成為localhost近端網路的程式,請參考這個連結。
      http://www.articulate.com/forums/articulate-presenter/3174-remove-activex-warning.html

      希望有幫到您 ^^

  3. 您好
    首先謝謝您分享的這份文章
    不過在使用上遇到一個問題

    我使用了部份預覽列印
    但是預覽的結果字型與對齊都會消失
    彷彿不曾設定過
    以下是我的程式碼



    ‘ 
    ‘ 

    ‘ 資料庫內容



    列印的部份與文章上相同
    以下是clean函數
    Function Clean(str As String)
    str=Replace(str,vbCrLf,"")
    Clean=Replace(str,chr(32),"  “)
    End Function

    但是按下列印的結果
    不管是置中對齊或是字體大小
    都和原本不同

    請問要如何解決

    • 您好,我測試了一下,是不會有這樣的問題發生喔,您好像有用到.Net的語法,可否把檔案寄給我nievorwei@gmail.com,我可以幫您測試看看。

  4. […] {int} autoSplit 自動分頁,預設為0 * @param {int} autoPrint 自動打印,預設為0 * @see https://nievor.wordpress.com/2008/07/31/javascript_print_preview/ */ function JsPrinter […]

  5. 我想請問
    這個列印程式可以控制列印的邊界嗎
    每次印出來左右都空一大片
    是否有方法改善呢

  6. 好用心喔!!
    謝謝分享喔~~~

  7. 感謝分享

  8. […] JavaScript列印頁面非常簡單,只需要一行就可以達成,更多詳細資料可以參考Javascript呼叫IE列印以及預覽列印的方法@小峰峰的筆記本。 […]

  9. 我在DIV裡放 好像按下去沒反應耶 會有影響嗎?

  10. I am really impressed with your writing skills as well as with the layout on your weblog.

    Is this a paid theme or did you modify it yourself? Anyway
    keep up the nice quality writing, it is rare to see a nice blog like
    this one today.

  11. You’re so awesome! I do not think I’ve read anything like that before.
    So wonderful to discover somebody with a few original
    thoughts on this subject. Seriously.. thank you
    for starting this up. This website is something that is needed on the web, someone with some originality!

  12. I’m truly enjoying the design and layout of your blog. It’s a very easy on the eyes which makes it much more enjoyable for me to come here and visit more often.
    Did you hire out a designer to create your theme?
    Great work!

  13. Everyone loves what you guys are up too. This kind of clever work and exposure!
    Keep up the great works guys I’ve included you guys to my blogroll.

  14. There are cheap shutters that are available in most online stores.
    Popular as a practical solution to any of the above, plantation shutters have also become widely used as for aesthetic
    purposes, adding style and individuality to the facade of a building.

    Some options include eyebrow, hexagon, quarter circle, oval, and arches: contact your window
    treatment fabricator for details.

  15. You really make it appear really easy together with your
    presentation but I in finding this matter to be actually something which I believe I
    might by no means understand. It kind of feels too complicated and extremely vast
    for me. I am taking a look forward to your subsequent put up, I’ll attempt to get the grasp of it!

  16. Today, I went to the beach with my children. I found a sea shell and gave it to my 4 year old
    daughter and said “You can hear the ocean if you put this to your ear." She put the shell to her ear
    and screamed. There was a hermit crab inside and it pinched her ear.
    She never wants to go back! LoL I know this is totally off topic but
    I had to tell someone!

  17. Greetings! I know this is somewhat off topic but I was wondering if you knew where I could find a captcha plugin for my comment form?
    I’m using the same blog platform as yours and I’m having problems finding one?
    Thanks a lot!

  18. Very good article! We will be linking to this particularly great content on our
    site. Keep up the great writing.

  19. Hey there would you mind stating which blog platform you’re using? I’m
    going to start my own blog soon but I’m having a difficult time selecting between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I’m
    looking for something completely unique. P.S Apologies for being off-topic but
    I had to ask!

  20. 不知道能不能將頁面縮小 符合 A4大小

  21. 能否將網頁內容自動縮小到符合列印範圍呢 例如 A4大小

  22. Hello there! Would you mind if I share your blog with my twitter group?
    There’s a lot of folks that I think would really appreciate your content.

    Please let me know. Many thanks


發表迴響

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

在 WordPress.com 建立免費網站或網誌.
Entries留言 feeds.

%d 位部落客按了讚: