From dd9705edd9c6c81192a3ef5bd1cd85eaa2331ca0 Mon Sep 17 00:00:00 2001 From: Sathvik P Date: Wed, 18 Jul 2012 15:19:11 +0530 Subject: [PATCH] Using cross-browser compatiable detection of whether an element is a DOM node, courtesy some@StackOverflow --- jQuery.print.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/jQuery.print.js b/jQuery.print.js index ddb8da3..8ffb143 100644 --- a/jQuery.print.js +++ b/jQuery.print.js @@ -9,9 +9,16 @@ $.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; - if (this instanceof HTMLElement) { + if (isNode(this)) { // If `this` is a HTML element, i.e. for // $(selector).print() $this = $(this); @@ -19,7 +26,7 @@ if (arguments.length > 0) { // $.print(selector,options) $this = $(arguments[0]); - if ($this[0] instanceof HTMLElement) { + if (isNode($this[0])) { if (arguments.length > 1) { options = arguments[1]; } @@ -91,6 +98,7 @@ $iframe.remove(); } } catch (e) { + // Use the pop-up method if iframe fails for some reason w = window.open(); w.document.write(content); w.print();