|
1 | 1 | /* @license |
2 | | - * jQuery.print, version 1.3.3 |
| 2 | + * jQuery.print, version 1.4.0 |
3 | 3 | * (c) Sathvik Ponangi, Doers' Guild |
4 | 4 | * Licence: CC-By (http://creativecommons.org/licenses/by/3.0/) |
5 | 5 | *--------------------------------------------------------------------------*/ |
|
19 | 19 | return jqObj; |
20 | 20 | } |
21 | 21 |
|
22 | | - function printFrame(frameWindow, timeout) { |
| 22 | + function printFrame(frameWindow, content, options) { |
23 | 23 | // Print the selected window/iframe |
24 | 24 | var def = $.Deferred(); |
25 | 25 | try { |
| 26 | + frameWindow = frameWindow.contentWindow || frameWindow.contentDocument || frameWindow; |
| 27 | + var wdoc = frameWindow.document || frameWindow.contentDocument || frameWindow; |
| 28 | + if(options.doctype) { |
| 29 | + wdoc.write(options.doctype); |
| 30 | + } |
| 31 | + wdoc.write(content); |
| 32 | + wdoc.close(); |
26 | 33 | setTimeout(function () { |
27 | 34 | // Fix for IE : Allow it to render the iframe |
28 | 35 | frameWindow.focus(); |
|
37 | 44 | } |
38 | 45 | frameWindow.close(); |
39 | 46 | def.resolve(); |
40 | | - }, timeout); |
| 47 | + }, options.timeout); |
41 | 48 | } catch (err) { |
42 | 49 | def.reject(err); |
43 | 50 | } |
44 | 51 | return def; |
45 | 52 | } |
46 | 53 |
|
47 | | - function printContentInNewWindow(content, timeout) { |
| 54 | + function printContentInIFrame(content, options) { |
| 55 | + var $iframe = $(options.iframe + ""); |
| 56 | + var iframeCount = $iframe.length; |
| 57 | + if (iframeCount === 0) { |
| 58 | + // Create a new iFrame if none is given |
| 59 | + $iframe = $('<iframe height="0" width="0" border="0" wmode="Opaque"/>') |
| 60 | + .prependTo('body') |
| 61 | + .css({ |
| 62 | + "position": "absolute", |
| 63 | + "top": -999, |
| 64 | + "left": -999 |
| 65 | + }); |
| 66 | + } |
| 67 | + var frameWindow = $iframe.get(0); |
| 68 | + return printFrame(frameWindow, content, options) |
| 69 | + .done(function () { |
| 70 | + // Success |
| 71 | + setTimeout(function () { |
| 72 | + // Wait for IE |
| 73 | + if (iframeCount === 0) { |
| 74 | + // Destroy the iframe if created here |
| 75 | + $iframe.remove(); |
| 76 | + } |
| 77 | + }, 100); |
| 78 | + }) |
| 79 | + .fail(function (err) { |
| 80 | + // Use the pop-up method if iframe fails for some reason |
| 81 | + console.error("Failed to print from iframe", err); |
| 82 | + printContentInNewWindow(content, options); |
| 83 | + }) |
| 84 | + .always(function () { |
| 85 | + try { |
| 86 | + options.deferred.resolve(); |
| 87 | + } catch (err) { |
| 88 | + console.warn('Error notifying deferred', err); |
| 89 | + } |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + function printContentInNewWindow(content, options) { |
48 | 94 | // Open a new window and print selected content |
49 | | - var w = window.open(); |
50 | | - w.document.write(content); |
51 | | - w.document.close(); |
52 | | - return printFrame(w, timeout); |
| 95 | + var frameWindow = window.open(); |
| 96 | + return printFrame(frameWindow, content, options) |
| 97 | + .always(function () { |
| 98 | + try { |
| 99 | + options.deferred.resolve(); |
| 100 | + } catch (err) { |
| 101 | + console.warn('Error notifying deferred', err); |
| 102 | + } |
| 103 | + }); |
53 | 104 | } |
54 | 105 |
|
55 | 106 | function isNode(o) { |
|
101 | 152 | manuallyCopyFormValues: true, |
102 | 153 | deferred: $.Deferred(), |
103 | 154 | timeout: 250, |
104 | | - title: null |
| 155 | + title: null, |
| 156 | + doctype: '<!doctype html>' |
105 | 157 | }; |
106 | 158 | // Merge with user-options |
107 | 159 | options = $.extend({}, defaults, (options || {})); |
|
177 | 229 | if (options.iframe) { |
178 | 230 | // Use an iframe for printing |
179 | 231 | try { |
180 | | - var $iframe = $(options.iframe + ""); |
181 | | - var iframeCount = $iframe.length; |
182 | | - if (iframeCount === 0) { |
183 | | - // Create a new iFrame if none is given |
184 | | - $iframe = $('<iframe height="0" width="0" border="0" wmode="Opaque"/>') |
185 | | - .prependTo('body') |
186 | | - .css({ |
187 | | - "position": "absolute", |
188 | | - "top": -999, |
189 | | - "left": -999 |
190 | | - }); |
191 | | - } |
192 | | - var w, wdoc; |
193 | | - w = $iframe.get(0); |
194 | | - w = w.contentWindow || w.contentDocument || w; |
195 | | - wdoc = w.document || w.contentDocument || w; |
196 | | - wdoc.open(); |
197 | | - wdoc.write(content); |
198 | | - wdoc.close(); |
199 | | - printFrame(w, options.timeout) |
200 | | - .done(function () { |
201 | | - // Success |
202 | | - setTimeout(function () { |
203 | | - // Wait for IE |
204 | | - if (iframeCount === 0) { |
205 | | - // Destroy the iframe if created here |
206 | | - $iframe.remove(); |
207 | | - } |
208 | | - }, 100); |
209 | | - }) |
210 | | - .fail(function (err) { |
211 | | - // Use the pop-up method if iframe fails for some reason |
212 | | - console.error("Failed to print from iframe", err); |
213 | | - printContentInNewWindow(content, options.timeout); |
214 | | - }) |
215 | | - .always(function () { |
216 | | - try { |
217 | | - options.deferred.resolve(); |
218 | | - } catch (err) { |
219 | | - console.warn('Error notifying deferred', err); |
220 | | - } |
221 | | - }); |
| 232 | + printContentInIFrame(content, options); |
222 | 233 | } catch (e) { |
223 | 234 | // Use the pop-up method if iframe fails for some reason |
224 | 235 | console.error("Failed to print from iframe", e.stack, e.message); |
225 | | - printContentInNewWindow(content, options.timeout) |
226 | | - .always(function () { |
227 | | - try { |
228 | | - options.deferred.resolve(); |
229 | | - } catch (err) { |
230 | | - console.warn('Error notifying deferred', err); |
231 | | - } |
232 | | - }); |
| 236 | + printContentInNewWindow(content, options); |
233 | 237 | } |
234 | 238 | } else { |
235 | 239 | // Use a new window for printing |
236 | | - printContentInNewWindow(content, options.timeout) |
237 | | - .always(function () { |
238 | | - try { |
239 | | - options.deferred.resolve(); |
240 | | - } catch (err) { |
241 | | - console.warn('Error notifying deferred', err); |
242 | | - } |
243 | | - }); |
| 240 | + printContentInNewWindow(content, options); |
244 | 241 | } |
245 | 242 | return this; |
246 | 243 | }; |
|
0 commit comments