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 439f0f9..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,7 +30,11 @@ } wdoc.write(content); wdoc.close(); - setTimeout(function () { + var printed = false; + function callPrint() { + if(printed) { + return; + } // Fix for IE : Allow it to render the iframe frameWindow.focus(); try { @@ -43,8 +47,13 @@ frameWindow.print(); } frameWindow.close(); + printed = true; def.resolve(); - }, options.timeout); + } + // 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); } @@ -151,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 "