mirror of
https://github.com/wahyd4/jQuery.print.git
synced 2026-08-09 04:46:31 +10:00
Enhanced iframe printing and support for IE; Resolves #1
This commit is contained in:
@@ -47,12 +47,6 @@ Currently this plugin supports the following options:
|
||||
- Acceptable-Values: URL-string
|
||||
- Function: URL of an external stylesheet to be included
|
||||
|
||||
####rejectWindow
|
||||
|
||||
- Default: `true`
|
||||
- Acceptable-Values: Boolean
|
||||
- Function: Whether the `document` object should be used intead of `window`
|
||||
|
||||
####noPrintSelector
|
||||
|
||||
- Default: `".no-print"`
|
||||
|
||||
+4
-4
@@ -40,7 +40,7 @@
|
||||
<p>
|
||||
Some random text.
|
||||
</p>
|
||||
<button class="print-link" onclick="$('#ele1').print()">
|
||||
<button class="print-link no-print" onclick="jQuery('#ele1').print()">
|
||||
Print this
|
||||
</button>
|
||||
</div>
|
||||
@@ -58,7 +58,7 @@
|
||||
<p class="no-print">
|
||||
Some more random text.
|
||||
</p>
|
||||
<button class="print-link" onclick="$.print('#ele3')">
|
||||
<button class="print-link" onclick="jQuery.print('#ele3')">
|
||||
Print this
|
||||
</button>
|
||||
</div>
|
||||
@@ -67,12 +67,12 @@
|
||||
<p>
|
||||
Some really random text.
|
||||
</p>
|
||||
<button class="print-link">
|
||||
<button class="print-link avoid-this">
|
||||
Print this
|
||||
</button>
|
||||
</div>
|
||||
<br/>
|
||||
<button class="print-link" onclick="$.print()">
|
||||
<button class="print-link" onclick="jQuery.print()">
|
||||
Print page
|
||||
</button>
|
||||
</div>
|
||||
|
||||
+72
-38
@@ -1,4 +1,4 @@
|
||||
/* jQuery.print, version 1.0.2
|
||||
/* jQuery.print, version 1.0.3
|
||||
* (c) Sathvik Ponangi, Doers' Guild
|
||||
* Licence: CC-By (http://creativecommons.org/licenses/by/3.0/)
|
||||
*--------------------------------------------------------------------------*/
|
||||
@@ -6,19 +6,42 @@
|
||||
(function($) {"use strict";
|
||||
// A nice closure for our definitions
|
||||
|
||||
function getjQueryObject(string) {
|
||||
// Make string a vaild jQuery thing
|
||||
var jqObj = $("");
|
||||
try {
|
||||
jqObj = $(string).clone();
|
||||
} catch(e) {
|
||||
jqObj = $("<span />").html(string);
|
||||
}
|
||||
return jqObj;
|
||||
}
|
||||
|
||||
function isNode(o) {
|
||||
/* http://stackoverflow.com/a/384380/937891 */
|
||||
return !!( typeof Node === "object" ? o instanceof Node : o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName === "string");
|
||||
}
|
||||
|
||||
|
||||
$.print = $.fn.print = function() {
|
||||
// Print a given set of elements
|
||||
|
||||
var isNode = function(o) {
|
||||
/* http://stackoverflow.com/a/384380/937891 */
|
||||
return ( typeof Node === "object" ? o instanceof Node : o && typeof o === "object" && typeof o.nodeType === "number" && typeof o.nodeName === "string");
|
||||
}
|
||||
var options, $this;
|
||||
var options, $this, self = this;
|
||||
|
||||
if (isNode(this)) {
|
||||
// console.log("Printing", this, arguments);
|
||||
|
||||
if ( self instanceof $) {
|
||||
// Get the node if it is a jQuery object
|
||||
self = self.get(0);
|
||||
}
|
||||
|
||||
if (isNode(self)) {
|
||||
// If `this` is a HTML element, i.e. for
|
||||
// $(selector).print()
|
||||
$this = $(this);
|
||||
$this = $(self);
|
||||
if (arguments.length > 0) {
|
||||
options = arguments[0];
|
||||
}
|
||||
} else {
|
||||
if (arguments.length > 0) {
|
||||
// $.print(selector,options)
|
||||
@@ -30,81 +53,92 @@
|
||||
} else {
|
||||
// $.print(options)
|
||||
options = arguments[0];
|
||||
$this = $(this);
|
||||
$this = $("html");
|
||||
}
|
||||
} else {
|
||||
// $.print()
|
||||
$this = $(this);
|
||||
$this = $("html");
|
||||
}
|
||||
}
|
||||
|
||||
// Default options
|
||||
var defaults = {
|
||||
globalStyles : true,
|
||||
mediaPrint : false,
|
||||
stylesheet : null,
|
||||
rejectWindow : true,
|
||||
noPrintSelector : ".no-print",
|
||||
iframe : true,
|
||||
append : null,
|
||||
prepend : null
|
||||
};
|
||||
// Default options
|
||||
options = $.extend(defaults, options);
|
||||
// Merge with user-options
|
||||
options = $.extend({}, defaults, (options || {}));
|
||||
|
||||
if (options.rejectWindow && $this[0] === window) {
|
||||
// Use document instead of window as it holds all HTML
|
||||
$this = $(document);
|
||||
}
|
||||
|
||||
var styles = $("");
|
||||
var $styles = $("");
|
||||
if (options.globalStyles) {
|
||||
// Apply the stlyes from the current sheet to the printed page
|
||||
styles = $("style, link, meta, title");
|
||||
$styles = $("style, link, meta, title");
|
||||
} else if (options.mediaPrint) {
|
||||
// Apply the media-print stylesheet
|
||||
styles = $("link[media=print]");
|
||||
$styles = $("link[media=print]");
|
||||
}
|
||||
if (options.stylesheet) {
|
||||
// Add a custom stylesheet if given
|
||||
styles = $.merge(styles, $('<link rel="stylesheet" href="' + options.stylesheet + '">'));
|
||||
$styles = $.merge($styles, $('<link rel="stylesheet" href="' + options.stylesheet + '">'));
|
||||
}
|
||||
|
||||
var copy = $this.clone();
|
||||
// Create a copy of the element to print
|
||||
var copy = $this.clone();
|
||||
// Wrap it in a span to get the HTML markup string
|
||||
copy = $("<span/>").append(copy);
|
||||
copy.find(options.noPrintSelector).remove();
|
||||
// Remove unwanted elements
|
||||
copy.append(styles.clone());
|
||||
copy.append($(options.append).clone());
|
||||
copy.prepend($(options.prepend).clone());
|
||||
copy.find(options.noPrintSelector).remove();
|
||||
// Add in the styles
|
||||
copy.append($styles.clone());
|
||||
// Appedned content
|
||||
copy.append(getjQueryObject(options.append));
|
||||
// Prepended content
|
||||
copy.prepend(getjQueryObject(options.prepend));
|
||||
// Get the HTML markup string
|
||||
var content = copy.html();
|
||||
// Destroy the copy
|
||||
copy.remove();
|
||||
|
||||
var w, wdoc;
|
||||
if (options.iframe) {
|
||||
// Use an iframe for printing
|
||||
try {
|
||||
$iframe = $(options.iframe + "");
|
||||
var $iframe = $(options.iframe + "");
|
||||
var iframeCount = $iframe.length;
|
||||
if (iframeCount === 0) {
|
||||
// Create a new iFrame if none is given
|
||||
$iframe = $('<iframe/>').appendTo('body').hide();
|
||||
$iframe = $('<iframe height="0" width="0" border="0" wmode="Opaque"/>').prependTo('body').css({
|
||||
"position" : "absolute",
|
||||
"top" : -999,
|
||||
"left" : -999
|
||||
});
|
||||
}
|
||||
w = $iframe[0];
|
||||
w = w.contentWindow || w.contentDocument;
|
||||
wdoc = w.document || w;
|
||||
w = $iframe.get(0);
|
||||
w = w.contentWindow || w.contentDocument || w;
|
||||
wdoc = w.document || w.contentDocument || w;
|
||||
wdoc.open();
|
||||
wdoc.write(content);
|
||||
wdoc.close();
|
||||
w.focus();
|
||||
w.print();
|
||||
if (iframeCount === 0) {
|
||||
// Destroy the iframe if created here
|
||||
$iframe.remove();
|
||||
}
|
||||
setTimeout(function() {
|
||||
// Fix for IE : Allow it to render the iframe
|
||||
w.focus();
|
||||
w.print();
|
||||
setTimeout(function() {
|
||||
// Fix for IE
|
||||
if (iframeCount === 0) {
|
||||
// Destroy the iframe if created here
|
||||
$iframe.remove();
|
||||
}
|
||||
}, 100);
|
||||
}, 250);
|
||||
} catch (e) {
|
||||
// Use the pop-up method if iframe fails for some reason
|
||||
console.error("Failed to print from iframe", e.stack, e.message);
|
||||
w = window.open();
|
||||
w.document.write(content);
|
||||
w.document.close();
|
||||
|
||||
Reference in New Issue
Block a user