forked from tanathos/jquery.jqprint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.jqprint-0.3.js
83 lines (69 loc) · 2.8 KB
/
jquery.jqprint-0.3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// -----------------------------------------------------------------------
// Eros Fratini - [email protected]
// jqprint 0.3
//
// - 07/08/2014 - added removal of javascript, added cleanup (bmass02), updated Opera support (used from Gemorroj's fork)
// - 19/06/2009 - some new implementations, added Opera support
// - 11/05/2009 - first sketch
//
// Printing plug-in for jQuery, evolution of jPrintArea: http://plugins.jquery.com/project/jPrintArea
// requires jQuery 1.11.x
//
// Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
//------------------------------------------------------------------------
(function($) {
var opt;
$.fn.jqprint = function (options) {
opt = $.extend({}, $.fn.jqprint.defaults, options);
var $element = (this instanceof jQuery) ? this : $(this);
var isOpera = window.opera !== undefined
var doc
if (isOpera)
{
var tab = window.open("","jqPrint-preview");
tab.document.open();
doc = tab.document;
}
else
{
var $iframe = $("<iframe />");
if (!opt.debug) { $iframe.css({ position: "absolute", width: "0px", height: "0px", left: "-600px", top: "-600px" }); }
$iframe.appendTo("body");
doc = $iframe[0].contentWindow.document;
}
if (opt.importCSS)
{
if ($("link[media=print]").length > 0)
{
$("link[media=print]").each( function() {
doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' media='print' />");
});
}
else
{
$("link").each( function() {
doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' />");
});
}
}
doc.write($element.prepare_and_load(opt.printContainer));
doc.close();
(isOpera ? tab : $iframe[0].contentWindow).focus();
setTimeout( function() { (isOpera ? tab : $iframe[0].contentWindow).print(); (opt.operaSupport ? tab.close() : $iframe.remove()) }, 1000);
}
$.fn.jqprint.defaults = {
debug: false,
importCSS: true,
printContainer: true
};
//Prepare by removing unwanted javascript in script tags to prevent scope errors with JQuery
$.fn.prepare_and_load = function(withPrintContainer) {
$this = $(this)
$('script').remove();
return (withPrintContainer ? $this.outer() : $this.html())
}
// Thanks to 9__, found at http://users.livejournal.com/9__/380664.html
jQuery.fn.outer = function() {
return $($('<div></div>').html(this.clone())).html();
}
})(jQuery);