1 // <--- --------------------------------------------------------------------------------------- ----
\r
4 // Ask Ben: Print Part Of A Web Page With jQuery
\r
7 // Ben Nadel / Kinky Solutions
\r
10 // http://www.bennadel.com/index.cfm?event=blog.view&id=1591
\r
13 // May 21, 2009 at 9:10 PM
\r
15 // ---- --------------------------------------------------------------------------------------- --->
\r
18 // Create a jquery plugin that prints the given element.
\r
19 jQuery.fn.print = function(){
\r
20 // NOTE: We are trimming the jQuery collection down to the
\r
21 // first element in the collection.
\r
22 if (this.size() > 1){
\r
23 this.eq( 0 ).print();
\r
25 } else if (!this.size()){
\r
29 var chart = $(this).closest('div.quintile-outer-container').find('div.jqplot-target');
\r
30 // var imgelem = chart.jqplotToImageElem();
\r
31 var imageElemStr = chart.jqplotToImageElemStr();
\r
32 // var statsrows = $(this).closest('div.quintile-outer-container').find('table.stats-table tr');
\r
33 var statsTable = $('<div></div>').append($(this).closest('div.quintile-outer-container').find('table.stats-table').clone());
\r
34 // var rowstyles = window.getComputedStyle(statsrows.get(0), '');
\r
36 // ASSERT: At this point, we know that the current jQuery
\r
37 // collection (as defined by THIS), contains only one
\r
38 // printable element.
\r
40 // Create a random name for the print frame.
\r
41 var strFrameName = ("printer-" + (new Date()).getTime());
\r
43 // Create an iFrame with the new name.
\r
44 var jFrame = $( "<iframe name='" + strFrameName + "'>" );
\r
46 // Hide the frame (sort of) and attach to the body.
\r
48 .css( "width", "1px" )
\r
49 .css( "height", "1px" )
\r
50 .css( "position", "absolute" )
\r
51 .css( "left", "-9999px" )
\r
52 .appendTo( $( "body:first" ) )
\r
55 // Get a FRAMES reference to the new frame.
\r
56 var objFrame = window.frames[ strFrameName ];
\r
58 // Get a reference to the DOM in the new frame.
\r
59 var objDoc = objFrame.document;
\r
61 // Grab all the style tags and copy to the new
\r
62 // document so that we capture look and feel of
\r
63 // the current document.
\r
65 // Create a temp document DIV to hold the style tags.
\r
66 // This is the only way I could find to get the style
\r
68 var jStyleDiv = $( "<div>" ).append(
\r
69 $( "style" ).clone()
\r
72 // Write the HTML for the document. In this, we will
\r
73 // write out the HTML of the current element.
\r
75 objDoc.write( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" );
\r
76 objDoc.write( "<html>" );
\r
77 objDoc.write( "<body>" );
\r
78 objDoc.write( "<head>" );
\r
79 objDoc.write( "<title>" );
\r
80 objDoc.write( document.title );
\r
81 objDoc.write( "</title>" );
\r
82 objDoc.write( jStyleDiv.html() );
\r
83 objDoc.write( "</head>" );
\r
85 // Typically, would just write out the html.
\r
86 // objDoc.write( this.html() );
\r
88 // We need to do specific manipulation for kcp quintiles.
\r
89 objDoc.write( '<div class="quintile-outer-container ui-widget ui-corner-all"> \
\r
90 <div class="quintile-content ui-widget-content ui-corner-bottom"> \
\r
91 <table class="quintile-display"> \
\r
93 <td class="chart-cell">');
\r
95 objDoc.write(imageElemStr);
\r
97 objDoc.write('</td> <td class="stats-cell">');
\r
99 objDoc.write(statsTable.html());
\r
101 objDoc.write('</td></tr></table></div></div>');
\r
103 objDoc.write( "</body>" );
\r
104 objDoc.write( "</html>" );
\r
108 // When the iframe is completely loaded, print it.
\r
109 // This seemed worked in IE 9, but caused problems in FF.
\r
111 // $(objFrame).load(function() {
\r
112 // objFrame.focus();
\r
113 // objFrame.print();
\r
117 // This works in all supported browsers.
\r
118 // Note, might have to adjust time.
\r
127 // Have the frame remove itself in about a minute so that
\r
128 // we don't build up too many of these frames.
\r