commit 7692c7a40c3fce9efc0b8d62ed022ebcb4e5fd2c Author: Sathvik P Date: Mon Jul 16 18:44:34 2012 +0530 Version 0.1 diff --git a/jQuery.print.js b/jQuery.print.js new file mode 100644 index 0000000..8b1993e --- /dev/null +++ b/jQuery.print.js @@ -0,0 +1,210 @@ +/* jQuery.print, version 0.1 + * (c) Sathvik Ponangi + * + *--------------------------------------------------------------------------*/ + +(function($) { + // A nice closure for our definitions + + $.fn.print = function() { + // Print a given set of elements + + var options, $this; + + if (this instanceof HTMLElement) { + // If `this` is a HTML element, i.e. for + // $(selector).print() + $this = $(this); + } else { + if (arguments.length > 0) { + // $.print(selector,options) + $this = $(arguments[0]); + if ($this[0] instanceof HTMLElement) { + if (arguments.length > 1) { + options = arguments[1]; + } + } else { + // $.print(options) + options = arguments[0]; + $this = $(this); + } + } else { + // $.print() + $this = $(this); + } + } + + var defaults = { + addGlobalStyles : true, + stylesheet : null, + rejectWindow : true, + noPrintSelector : ".no-print", + iframe : null, + append : null, + prepend : null + }; // Default options + options = $.extend(defaults, options); // Merge with user-options + + if (options.rejectWindow && $this[0] === window) { + // Use document instead of window as it holds all HTML + $this = $(document); + } + + var styles = $(""); + if (options.addGlobalStyles) { + // Apply the stlyes from the current sheet to the printed page + styles = $("style, link"); + } + if (options.stylesheet) { + // Add a custom stylesheet if given + styles = $.merge(styles, $('')); + } + + var copy = $this.clone(); // Create a copy of the element to print + copy = $("").append(copy); + copy.find(options.noPrintSelector).remove();// Remove unwanted elements + copy.append(styles.clone()); + copy.append(options.append); + copy.prepend(options.prepend); + var content = copy.html(); + copy.remove(); + + var w; + if (options.iframe) { + // Use an iframe for printing + $iframe = $(options.iframe); + iframeCount = $iframe.length; + if (iframeCount === 0) { + // Create a new iFrame if none is given + $iframe = $('