-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAreaPrinter.min.js
19 lines (19 loc) · 1.59 KB
/
AreaPrinter.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* @describe: HTMl 区域打印,纯JS
* @version 0.1
* @date created: 7/23/2013 15:10:23
* @author chyxion
* @support: [email protected]
* @date modified:
* @modified by:
* How to use:
* Print Area: <div id="PrintArea"> ... html ... </div>
* JavaScript: <script>
* new AreaPrinter('PrintArea').print();
* </script>
*/
/**
* 调用方式,new AreaPrinter('PrintAreaID').print();
*@param 打印区域id,
*/
function AreaPrinter(e){this.id=e}AreaPrinter.prototype={get_head:function(){var e="<head>",t=document.getElementsByTagName("link"),n,r;if(t.length)for(r=0;r<t.length;++r)n=t[r],n.rel&&n.rel.toLowerCase()=="stylesheet"&&n.media&&n.media.toLowerCase()=="print"&&(e+='<link type="text/css" rel="stylesheet" href="'+n.href+'" media="print">');return e+"</head>"},get_body:function(){var e=this,t=document.getElementById(e.id);return'<body onload="focus(); print();"><div id="'+e.id+'" class="'+(t.className||"")+'" style="'+(t.style.cssText||"")+'">'+t.innerHTML+"</div></body>"},build_iframe:function(){var e;try{e=document.createElement("iframe"),document.body.appendChild(e),e.style.cssText="border:0;position:absolute;width:0px;height:0px;left:0px;top:0px;",e.src="about:blank",e.doc=e.contentDocument?e.contentDocument:e.contentWindow?e.contentWindow.document:e.document}catch(t){throw t+". IFrames may not be supported in this browser."}if(!e.doc)throw"Cannot find document.";return e},print:function(){var e=this,t=e.build_iframe();t.doc.open(),t.doc.write("<!DOCTYPE html><html>"+e.get_head()+e.get_body()+"</html>"),t.doc.close(),t.parentNode.removeChild(t)}};