Archive for 五月, 2009

[轉貼] Javascript呼叫IE列印以及預覽列印的方法

Javascript呼叫IE列印以及預覽列印的方法
七月 31, 2008 at 12:46 午後 | In Javascript/HTML | 7 Comments
Tags: div, 部分預覽, 部分列印, HTML, Javascript, Preview, Print
剛好有機會用到,所以我整理了Javascript關於IE列印的一些寫法,一般比較少見的,就是區塊列印以及區塊預覽列印,可能很多人都不曉得要怎麼處 理,以下是我查了一些語法資料,然後自己寫個sample測試出來的code,我的寫法不一定是最好的,如果大家如果有發現更好的寫法,希望可以互相交流 一下。

整頁列印:
html部分:

Javascript部分:
N/A

部分列印(只列印div包起來的網頁):
html部分:
</p> <div id="block"><img src="http://www.ruten.com.tw/imgs/2008/logo.gif" mce_src="http://www.ruten.com.tw/imgs/2008/logo.gif" alt="" /></div> <p>
Javascript部分:
//列印div包起來的部分並且列印完畢後自動關閉列印網頁
function printScreen(block){
var value = block.innerHTML;
var printPage = window.open(“","printPage","");
printPage.document.open();
printPage.document.write(“");
printPage.document.write("

");
printPage.document.write(value);
printPage.document.write("

“);
printPage.document.close(“");
}

整頁預覽列印:
html部分:

Javascript部分:
N/A

部分預覽列印(只預覽div包起來的部份):
html部分:

Javascript部分:
//預覽div包起來的部分並且列印完畢後自動關閉列印網頁
function previewScreen(block){
var value = block.innerHTML;
var printPage = window.open(“","printPage","");
printPage.document.open();
printPage.document.write(“");
printPage.document.write(“");
printPage.document.write("

");
printPage.document.write(value);
printPage.document.write("

“);
printPage.document.close(“");
}

[轉貼][Javascript] 實現網頁列印 自動分頁 自動列印

http://blog.phptw.idv.tw/read-177.html

/**
* @package js.printer
* @author wenchi
* @version 20080821
*
* IE 5.5 6.0 7.0 測試OK
* firefox 部份功能不適用
* Opera , Safari 未測試
*
*/

/**
* HOWTO:
*
* 第一種方式:
*
* 1. 於 之間加入
*
*
*
* var JsPrinter=new JsPrinter();
*
*
* 2. 於 之間 加入
*
* 分頁
* 預覽列印
* 列印
* 設定印表機
*
* 3. 於要分頁的節點加入

*
*
*
* 第二種方式:
*
* 1. 於要分頁的節點加入

*
* 2. 於 之前 加入
*
*
* var JsPrinter=new JsPrinter(1,1); // 自動分頁,自動列印
* var JsPrinter=new JsPrinter(1); // 自動分頁,自動列印
*
*
*
*
* Object Public Method :
* 1. JsPrinter.print(); // 列印
* 2. JsPrinter.prview(); // 預覽列印
* 以上 IE && FireFox 適用
*
* 3. JsPrinter.splitPage(); // 分頁
* 4. JsPrinter.setPrint(); // 設定印表機
* 以上 IE 適用 , FireFox 不適用
*
*
*
* 註:
* 1. 如果要使用完整功能,請於 之後加入
*
* 2. Firefox 如果要使用完整功能 請安裝 Firefox IE核心
*
*
*/

/**
* WebBrowser 說明
* IE 5.5 6.0 7.0 測試OK
* firefox不適用
* Opera , Safari 未測試
*
*
* WebBrowser.ExecWB(1,1) 打開
* WebBrowser.ExecWB(2,1) 關閉現在所有的IE窗口,並打開一個新窗口
* WebBrowser.ExecWB(4,1) 保存網頁
* WebBrowser.ExecWB(6,1) 打印
* WebBrowser.ExecWB(7,1) 打印預覽
* WebBrowser.ExecWB(8,1) 打印頁面設置
* WebBrowser.ExecWB(10,1) 查看頁面屬性
* WebBrowser.ExecWB(15,1) 好像是撤銷,有待確認
* WebBrowser.ExecWB(17,1) 全選
* WebBrowser.ExecWB(22,1) 刷新
* WebBrowser.ExecWB(45,1) 關閉窗體無提示
*
*/

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

var autoSplilt,autoPrint;

if (!autoSplit) this.autoSplilt=0;
else this.autoSplilt=Math.max(eval(autoSplit),0);

if (!autoPrint) this.autoPrint=0;
else this.autoPrint=Math.max(eval(autoSplit),0);

if (this.autoSplilt==1) this.__splitPage();
if (this.autoPrint==1) this.print();

return true;

}

/** private member **/
JsPrinter.prototype.__tag="JsPrintSplit";
//JsPrint.prototype.autoSplilt=1;
//JsPrint.prototype.autoPrint=1;

/**
* do change it
*/
JsPrinter.prototype.isIE = document.all ? 1 : 0
JsPrinter.prototype.isNs4 = document.layers ? 1 : 0
JsPrinter.prototype.isDom = document.getElementById ? 1 : 0
JsPrinter.prototype.isMac = navigator.platform == “MacPPC"
JsPrinter.prototype.isMo5 = document.getElementById && !document.all ? 1 : 0
JsPrinter.prototype.isWebBrowser=false;
JsPrinter.prototype.totalPage=0;

JsPrinter.prototype.print = function (){
window.print();
}

JsPrinter.prototype.prview = function (){

if (this.isWebBrowser==false) this.__getWebBrowser();

if (this.isMo5 == 1 || this.isWebBrowser==0) {
alert(“WebBrowser not support print View");
return false;
}
WebBrowser.ExecWB(7,1);
}

JsPrinter.prototype.setPrint = function (){

if (this.isWebBrowser==false) this.__getWebBrowser();

if (this.isMo5 == 1 || this.isWebBrowser==0) {
alert(“WebBrowser not support this function");
return false;
}
WebBrowser.ExecWB(8,1);

}

JsPrinter.prototype.splitPage = function (){
this.__splitPage();
}

JsPrinter.prototype.unSplitPage = function (){
this.__splitPage(false);
}

JsPrinter.prototype.setTag = function (tag){
this.__tag=tag;
}

/**
* private method
*/
JsPrinter.prototype.__getWebBrowser = function (){
this.isWebBrowser = document.getElementById(“WebBrowser") ? 1 : 0
}

/**
* private method
* @see http://www.w3schools.com/HTMLDOM/prop_style_pagebreakbefore.asp
*/
JsPrinter.prototype.__splitPage = function (disable){

if (!disable) var isSpilt="always";
else var isSpilt="avoid";

var rows=document.getElementsByTagName(“div");

for (var i=0;i<rows.length;i++){
if (rows[i].id==this.__tag){
rows[i].style.pageBreakBefore="always";
this.totalPage++;
}
}

}