12 Commits
5 changed files with 88 additions and 32 deletions
+28 -11
View File
@@ -5,30 +5,40 @@ jQuery.print is a plugin for printing specific parts of a page
## Usage ## Usage
Include it in your HTML after importing jQuery, like: Include it in your HTML after importing jQuery, like:
```html
<script type="text/JavaScript" src="path/to/jquery.print.js" /> <script type="text/JavaScript" src="path/to/jquery.print.js" />
```
Use it like: Use it like:
```js
$("#myElementId").print(/*options*/); $("#myElementId").print(/*options*/);
```
or or
```js
$.print("#myElementId" /*, options*/); $.print("#myElementId" /*, options*/);
```
You can submit the options object like: You can submit the options object like:
```js
$("#myElementId").print({ $("#myElementId").print({
globalStyles: true, globalStyles: true,
mediaPrint: false, mediaPrint: false,
stylesheet: null, stylesheet: null,
noPrintSelector: ".no-print", noPrintSelector: ".no-print",
iframe: true, iframe: true,
append: null, append: null,
prepend: null, prepend: null,
manuallyCopyFormValues: true, manuallyCopyFormValues: true,
deferred: $.Deferred() deferred: $.Deferred(),
timeout: 250
}); });
```
Currently this plugin supports the following options: Currently this plugin supports the following options:
####globalStyles ####globalStyles
@@ -79,6 +89,13 @@ Currently this plugin supports the following options:
- Acceptable-Values: Any valid `jQuery.Deferred` object - Acceptable-Values: Any valid `jQuery.Deferred` object
- Function: A jQuery.Deferred object that is resolved once the print function is called - Function: A jQuery.Deferred object that is resolved once the print function is called
####timeout
- Default: `250`
- 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
## Tested with ## Tested with
### jQuery ### jQuery
+2 -2
View File
@@ -1,7 +1,7 @@
{ {
"name": "jQuery.print", "name": "jQuery.print",
"main": "jQuery.print.js", "main": "jQuery.print.js",
"version": "1.3.0", "version": "1.3.3",
"homepage": "https://doersguild.github.io/jQuery.print/", "homepage": "https://doersguild.github.io/jQuery.print/",
"authors": [ "authors": [
"Sathvik P <sathvik@doersguild.com>" "Sathvik P <sathvik@doersguild.com>"
@@ -23,4 +23,4 @@
"test", "test",
"tests" "tests"
] ]
} }
+5 -5
View File
@@ -98,10 +98,10 @@
iframe : false, iframe : false,
//Don't print this //Don't print this
noPrintSelector : ".avoid-this", noPrintSelector : ".avoid-this",
//Add this on top //Add this at top
append : "Hello World!!!<br/>", prepend : "Hello World!!!<br/>",
//Add this at bottom //Add this on bottom
prepend : "<br/>Buh Bye!" append : "<br/>Buh Bye!"
}); });
}); });
// Fork https://github.com/sathvikp/jQuery.print for the full list of options // Fork https://github.com/sathvikp/jQuery.print for the full list of options
@@ -109,4 +109,4 @@
//]]> //]]>
</script> </script>
</body> </body>
</html> </html>
+21 -14
View File
@@ -1,5 +1,5 @@
/* @license /* @license
* jQuery.print, version 1.3.1 * jQuery.print, version 1.3.3
* (c) Sathvik Ponangi, Doers' Guild * (c) Sathvik Ponangi, Doers' Guild
* Licence: CC-By (http://creativecommons.org/licenses/by/3.0/) * Licence: CC-By (http://creativecommons.org/licenses/by/3.0/)
*--------------------------------------------------------------------------*/ *--------------------------------------------------------------------------*/
@@ -19,7 +19,7 @@
return jqObj; return jqObj;
} }
function printFrame(frameWindow) { function printFrame(frameWindow, timeout) {
// Print the selected window/iframe // Print the selected window/iframe
var def = $.Deferred(); var def = $.Deferred();
try { try {
@@ -37,19 +37,19 @@
} }
frameWindow.close(); frameWindow.close();
def.resolve(); def.resolve();
}, 250); }, timeout);
} catch (err) { } catch (err) {
def.reject(err); def.reject(err);
} }
return def; return def;
} }
function printContentInNewWindow(content) { function printContentInNewWindow(content, timeout) {
// Open a new window and print selected content // Open a new window and print selected content
var w = window.open(); var w = window.open();
w.document.write(content); w.document.write(content);
w.document.close(); w.document.close();
return printFrame(w); return printFrame(w, timeout);
} }
function isNode(o) { function isNode(o) {
@@ -99,7 +99,8 @@
append: null, append: null,
prepend: null, prepend: null,
manuallyCopyFormValues: true, manuallyCopyFormValues: true,
deferred: $.Deferred() deferred: $.Deferred(),
timeout: 250
}; };
// Merge with user-options // Merge with user-options
options = $.extend({}, defaults, (options || {})); options = $.extend({}, defaults, (options || {}));
@@ -132,20 +133,26 @@
if (options.manuallyCopyFormValues) { if (options.manuallyCopyFormValues) {
// Manually copy form values into the HTML for printing user-modified input fields // Manually copy form values into the HTML for printing user-modified input fields
// http://stackoverflow.com/a/26707753 // http://stackoverflow.com/a/26707753
copy.find("input, select, textarea") copy.find("input")
.each(function () { .each(function () {
var $field = $(this); var $field = $(this);
if ($field.is("[type='radio']") || $field.is("[type='checkbox']")) { if ($field.is("[type='radio']") || $field.is("[type='checkbox']")) {
if ($field.prop("checked")) { if ($field.prop("checked")) {
$field.attr("checked", "checked"); $field.attr("checked", "checked");
} }
} else if ($field.is("select")) {
$field.find(":selected")
.attr("selected", "selected");
} else { } else {
$field.attr("value", $field.val()); $field.attr("value", $field.val());
} }
}); });
copy.find("select").each(function () {
var $field = $(this);
$field.find(":selected").attr("selected", "selected");
});
copy.find("textarea").each(function () {
// Fix for https://github.com/DoersGuild/jQuery.print/issues/18#issuecomment-96451589
var $field = $(this);
$field.text($field.val());
});
} }
// Get the HTML markup string // Get the HTML markup string
var content = copy.html(); var content = copy.html();
@@ -179,7 +186,7 @@
wdoc.open(); wdoc.open();
wdoc.write(content); wdoc.write(content);
wdoc.close(); wdoc.close();
printFrame(w) printFrame(w, options.timeout)
.done(function () { .done(function () {
// Success // Success
setTimeout(function () { setTimeout(function () {
@@ -193,7 +200,7 @@
.fail(function (err) { .fail(function (err) {
// Use the pop-up method if iframe fails for some reason // Use the pop-up method if iframe fails for some reason
console.error("Failed to print from iframe", err); console.error("Failed to print from iframe", err);
printContentInNewWindow(content); printContentInNewWindow(content, options.timeout);
}) })
.always(function () { .always(function () {
try { try {
@@ -205,7 +212,7 @@
} catch (e) { } catch (e) {
// Use the pop-up method if iframe fails for some reason // Use the pop-up method if iframe fails for some reason
console.error("Failed to print from iframe", e.stack, e.message); console.error("Failed to print from iframe", e.stack, e.message);
printContentInNewWindow(content) printContentInNewWindow(content, options.timeout)
.always(function () { .always(function () {
try { try {
options.deferred.resolve(); options.deferred.resolve();
@@ -216,7 +223,7 @@
} }
} else { } else {
// Use a new window for printing // Use a new window for printing
printContentInNewWindow(content) printContentInNewWindow(content, options.timeout)
.always(function () { .always(function () {
try { try {
options.deferred.resolve(); options.deferred.resolve();
+32
View File
@@ -0,0 +1,32 @@
{
"name": "jquery.print",
"filename": "jQuery.print.min.js",
"version": "1.3.3",
"main": "jQuery.print.js",
"homepage": "https://doersguild.github.io/jQuery.print/",
"authors": [
"Sathvik P <sathvik@doersguild.com>"
],
"description": "Easy to use, Element Printing Plugin for jQuery, for printing specific parts of a page",
"keywords": [
"print",
"element printing",
"jquery print"
],
"dependencies": {
"jquery": ">=1.7.2"
},
"license": "CC-BY",
"repository": {
"type": "git",
"url": "git://github.com/DoersGuild/jQuery.print.git"
},
"autoupdate": {
"source": "git",
"target": "git://github.com/DoersGuild/jQuery.print.git",
"basePath": "",
"files": [
"jQuery.print*"
]
}
}