1// Create a jquery plugin that prints the given element. 2jQuery.fn.print = function(){ 3 // NOTE: We are trimming the jQuery collection down to the 4 // first element in the collection. 5 if (this.size() > 1){ 6 this.eq( 0 ).print(); 7 return; 8 } else if (!this.size()){ 9 return; 10 } 11 12 // ASSERT: At this point, we know that the current jQuery 13 // collection (as defined by THIS), contains only one 14 // printable element. 15 16 // Create a random name for the print frame. 17 var strFrameName = ("printer-" + (new Date()).getTime()); 18 19 // Create an iFrame with the new name. 20 var jFrame = jQuery( "<iframe name='" + strFrameName + "'>" ); 21 22 // Hide the frame (sort of) and attach to the body. 23 jFrame 24 .css( "width", "1px" ) 25 .css( "height", "1px" ) 26 .css( "position", "absolute" ) 27 .css( "left", "-9999px" ) 28 .appendTo( jQuery( "body:first" ) ) 29 ; 30 31 // Get a FRAMES reference to the new frame. 32 var objFrame = window.frames[ strFrameName ]; 33 34 // Get a reference to the DOM in the new frame. 35 var objDoc = objFrame.document; 36 37 // Grab all the style tags and copy to the new 38 // document so that we capture look and feel of 39 // the current document. 40 41 // Create a temp document DIV to hold the style tags. 42 // This is the only way I could find to get the style 43 // tags into IE. 44 var jStyleDiv = jQuery( "<div>" ).append( 45 jQuery( "style" ).clone() 46 ); 47 48 // Write the HTML for the document. In this, we will 49 // write out the HTML of the current element. 50 objDoc.open(); 51 objDoc.write( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" ); 52 objDoc.write( "<html>" ); 53 objDoc.write( "<body>" ); 54 objDoc.write( "<head>" ); 55 objDoc.write( "<title>" ); 56 objDoc.write( document.title ); 57 objDoc.write( "</title>" ); 58 objDoc.write( jStyleDiv.html() ); 59 objDoc.write( "</head>" ); 60 objDoc.write( this.html() ); 61 objDoc.write( "</body>" ); 62 objDoc.write( "</html>" ); 63 objDoc.close(); 64 65 // Print the document. 66 objFrame.focus(); 67 objFrame.print(); 68 69 // Have the frame remove itself in about a minute so that 70 // we don't build up too many of these frames. 71 setTimeout( 72 function(){ 73 jFrame.remove(); 74 }, 75 (60 * 1000) 76 ); 77}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.