From f60d105905577c81ca981f2628cf49a5bc9e0f11 Mon Sep 17 00:00:00 2001 From: Villentrenmerth Date: Mon, 22 Aug 2016 20:09:17 +0200 Subject: [PATCH 1/2] Added onload event before printing Added on load event for frame window to wait for i.e. css to load. Problem occurs on slower internet connection when using additional fonts. Printed document without waiting for everything to load results with blank pages. --- jQuery.print.js | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/jQuery.print.js b/jQuery.print.js index 439f0f9..895185f 100644 --- a/jQuery.print.js +++ b/jQuery.print.js @@ -30,21 +30,24 @@ } wdoc.write(content); wdoc.close(); - setTimeout(function () { - // Fix for IE : Allow it to render the iframe - frameWindow.focus(); - try { - // Fix for IE11 - printng the whole page instead of the iframe content - if (!frameWindow.document.execCommand('print', false, null)) { - // document.execCommand returns false if it failed -http://stackoverflow.com/a/21336448/937891 + //Fix to give enough time for css to load + $(frameWindow).on("load", function() { + setTimeout(function () { + // Fix for IE : Allow it to render the iframe + frameWindow.focus(); + try { + // Fix for IE11 - printng the whole page instead of the iframe content + if (!frameWindow.document.execCommand('print', false, null)) { + // document.execCommand returns false if it failed -http://stackoverflow.com/a/21336448/937891 + frameWindow.print(); + } + } catch (e) { frameWindow.print(); } - } catch (e) { - frameWindow.print(); - } - frameWindow.close(); - def.resolve(); - }, options.timeout); + frameWindow.close(); + def.resolve(); + }, options.timeout); + }); } catch (err) { def.reject(err); } From 3e9462cd790f3e4b153fb061b6566b5358e7e965 Mon Sep 17 00:00:00 2001 From: Sathvik P Date: Thu, 15 Sep 2016 19:05:50 +0530 Subject: [PATCH 2/2] Support the load event and set the timeout option as its fallback --- README.md | 6 +++--- bower.json | 2 +- demo/index.html | 25 +++++++++++++++++++++++-- jQuery.print.js | 42 ++++++++++++++++++++++++------------------ package.json | 2 +- 5 files changed, 52 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 8de90ad..87d2f38 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ You can submit the options object like: prepend: null, manuallyCopyFormValues: true, deferred: $.Deferred(), - timeout: 250, + timeout: 750, title: null, doctype: '' }); @@ -93,9 +93,9 @@ Currently this plugin supports the following options: ####timeout - - Default: `250` + - Default: `750` - Acceptable-Values: Time in Milliseconds for `setTimeout` - - Function: To change the amount of time to wait for the content, etc to load before printing the element from the new window/iframe created + - Function: To change the amount of max time to wait for the content, etc to load before printing the element from the new window/iframe created, as a fallback if the [`load` event](https://developer.mozilla.org/en-US/docs/Web/Events/load) for the new window/iframe has not fired yet ####title diff --git a/bower.json b/bower.json index 466ae5c..00c93d0 100644 --- a/bower.json +++ b/bower.json @@ -1,7 +1,7 @@ { "name": "jQuery.print", "main": "jQuery.print.js", - "version": "1.4.1", + "version": "1.5.0", "homepage": "https://doersguild.github.io/jQuery.print/", "authors": [ "Sathvik P " diff --git a/demo/index.html b/demo/index.html index 9dac330..def2c01 100644 --- a/demo/index.html +++ b/demo/index.html @@ -47,7 +47,7 @@

Element 2

Some other random text.

@@ -64,13 +64,34 @@

Element 4

Some really random text.

+

+                $("#ele4").find('button').on('click', function() {
+                    //Print ele4 with custom options
+                    $("#ele4").print({
+                        //Use Global styles
+                        globalStyles : false,
+                        //Add link with attrbute media=print
+                        mediaPrint : false,
+                        //Custom stylesheet
+                        stylesheet : "http://fonts.googleapis.com/css?family=Inconsolata",
+                        //Print in a hidden iframe
+                        iframe : false,
+                        //Don't print this
+                        noPrintSelector : ".avoid-this",
+                        //Add this at top
+                        prepend : "Hello World!!!
", + //Add this on bottom + append : "
Buh Bye!" + }); + }); +

diff --git a/jQuery.print.js b/jQuery.print.js index 895185f..c3031f9 100644 --- a/jQuery.print.js +++ b/jQuery.print.js @@ -1,5 +1,5 @@ /* @license - * jQuery.print, version 1.4.0 + * jQuery.print, version 1.5.0 * (c) Sathvik Ponangi, Doers' Guild * Licence: CC-By (http://creativecommons.org/licenses/by/3.0/) *--------------------------------------------------------------------------*/ @@ -30,24 +30,30 @@ } wdoc.write(content); wdoc.close(); - //Fix to give enough time for css to load - $(frameWindow).on("load", function() { - setTimeout(function () { - // Fix for IE : Allow it to render the iframe - frameWindow.focus(); - try { - // Fix for IE11 - printng the whole page instead of the iframe content - if (!frameWindow.document.execCommand('print', false, null)) { - // document.execCommand returns false if it failed -http://stackoverflow.com/a/21336448/937891 - frameWindow.print(); - } - } catch (e) { + var printed = false; + function callPrint() { + if(printed) { + return; + } + // Fix for IE : Allow it to render the iframe + frameWindow.focus(); + try { + // Fix for IE11 - printng the whole page instead of the iframe content + if (!frameWindow.document.execCommand('print', false, null)) { + // document.execCommand returns false if it failed -http://stackoverflow.com/a/21336448/937891 frameWindow.print(); } - frameWindow.close(); - def.resolve(); - }, options.timeout); - }); + } catch (e) { + frameWindow.print(); + } + frameWindow.close(); + printed = true; + def.resolve(); + } + // Print once the frame window loads - seems to work for the new-window option but unreliable for the iframe + $(frameWindow).on("load", callPrint); + // Fallback to printing directly if the frame doesn't fire the load event for whatever reason + setTimeout(callPrint, options.timeout); } catch (err) { def.reject(err); } @@ -154,7 +160,7 @@ prepend: null, manuallyCopyFormValues: true, deferred: $.Deferred(), - timeout: 250, + timeout: 750, title: null, doctype: '' }; diff --git a/package.json b/package.json index 902c288..789f070 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "jQuery.print", "filename": "jQuery.print.min.js", - "version": "1.4.1", + "version": "1.5.0", "homepage": "https://doersguild.github.io/jQuery.print/", "authors": [ "Sathvik P "