PageSourceSearch

https://enying.eu/js/jquery.print.js

js enying.eu collected 2026-10-02 21:21:02 UTC 3,148 bytes, 121 lines download raw bytes

1/*  jQuery.print, version 1.01
2 *  (c) Sathvik Ponangi
3 * Licence: CC-By (http://creativecommons.org/licenses/by/3.0/)
4 *--------------------------------------------------------------------------*/
5
6(function($) {
7	// A nice closure for our definitions
8
9	$.print = $.fn.print = function() {
10		// Print a given set of elements
11
12		var isNode = function(o) {
13			/* http://stackoverflow.com/a/384380/937891 */
14			return (typeof Node === "object" ? o instanceof Node : o
15					&& typeof o === "object" && typeof o.nodeType === "number"
16					&& typeof o.nodeName === "string");
17		}
18
19		var options, $this;
20
21		if (isNode(this)) {
22			// If `this` is a HTML element, i.e. for
23			// $(selector).print()
24			$this = $(this);
25		} else {
26			if (arguments.length > 0) {
27				// $.print(selector,options)
28				$this = $(arguments[0]);
29				if (isNode($this[0])) {
30					if (arguments.length > 1) {
31						options = arguments[1];
32					}
33				} else {
34					// $.print(options)
35					options = arguments[0];
36					$this = $(this);
37				}
38			} else {
39				// $.print()
40				$this = $(this);
41			}
42		}
43
44		var defaults = {
45			globalStyles : true,
46			mediaPrint:false,
47			stylesheet : null,
48			rejectWindow : true,
49			noPrintSelector : ".no-print",
50			iframe : true,
51			append : null,
52			prepend : null
53		}; // Default options
54		options = $.extend(defaults, options); // Merge with user-options
55
56		if (options.rejectWindow && $this[0] === window) {
57			// Use document instead of window as it holds all HTML
58			$this = $(document);
59		}
60
61		var styles = $("");
62		if (options.globalStyles) {
63			// Apply the stlyes from the current sheet to the printed page
64			styles = $("style, link");
65		} else if (options.mediaPrint) {
66			// Apply the media-print stylesheet
67			styles = $("link[media=print]");
68		}
69		if (options.stylesheet) {
70			// Add a custom stylesheet if given
71			styles = $.merge(styles, $('<link rel="stylesheet" href="'
72					+ options.stylesheet + '">'));
73		}
74
75		var copy = $this.clone(); // Create a copy of the element to print
76		copy = $("<span/>").append(copy);
77		copy.find(options.noPrintSelector).remove();// Remove unwanted elements
78		copy.append(styles.clone());
79		copy.append($(options.append).clone());
80		copy.prepend($(options.prepend).clone());
81		var content = copy.html();
82		copy.remove();
83
84		var w, wdoc;
85		if (options.iframe) {
86			// Use an iframe for printing
87			try {
88				$iframe = $(options.iframe + "");
89				var iframeCount = $iframe.length;
90				if (iframeCount === 0) {
91					// Create a new iFrame if none is given
92					$iframe = $('<iframe/>').appendTo('body').hide();
93				}
94				w = $iframe[0];
95				w = w.contentWindow || w.contentDocument;
96				wdoc = w.document || w;
97				wdoc.open();
98				wdoc.write(content);
99				w.print();
100				if (iframeCount === 0) {
101					// Destroy the iframe if created here
102					$iframe.remove();
103				}
104			} catch (e) {
105				// Use the pop-up method if iframe fails for some reason
106				w = window.open();
107				w.document.write(content);
108				w.print();
109				w.close();
110			}
111		} else {
112			// Use a new window for printing
113			w = window.open();
114			w.document.write(content);
115			w.print();
116			w.close();
117		}
118		return this;
119	};
120
121})(jQuery);

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.