mirror of
https://github.com/wahyd4/jQuery.print.git
synced 2026-08-09 12:56:08 +10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
43daec10ef | ||
|
|
5ab3f500df | ||
|
|
a690a4b513 | ||
|
|
38f12da807 | ||
|
|
1b8610b182 | ||
|
|
fdf979b239 | ||
|
|
bf8dd435fb | ||
|
|
1a5fae505e | ||
|
|
c6a57a9cbe | ||
|
|
d7f9019356 | ||
|
|
62edb79737 |
@@ -5,30 +5,40 @@ jQuery.print is a plugin for printing specific parts of a page
|
||||
## Usage
|
||||
|
||||
Include it in your HTML after importing jQuery, like:
|
||||
|
||||
```html
|
||||
<script type="text/JavaScript" src="path/to/jquery.print.js" />
|
||||
```
|
||||
|
||||
Use it like:
|
||||
|
||||
```js
|
||||
$("#myElementId").print(/*options*/);
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```js
|
||||
$.print("#myElementId" /*, options*/);
|
||||
|
||||
```
|
||||
|
||||
You can submit the options object like:
|
||||
|
||||
```js
|
||||
$("#myElementId").print({
|
||||
globalStyles: true,
|
||||
mediaPrint: false,
|
||||
stylesheet: null,
|
||||
noPrintSelector: ".no-print",
|
||||
iframe: true,
|
||||
append: null,
|
||||
prepend: null,
|
||||
manuallyCopyFormValues: true,
|
||||
deferred: $.Deferred()
|
||||
globalStyles: true,
|
||||
mediaPrint: false,
|
||||
stylesheet: null,
|
||||
noPrintSelector: ".no-print",
|
||||
iframe: true,
|
||||
append: null,
|
||||
prepend: null,
|
||||
manuallyCopyFormValues: true,
|
||||
deferred: $.Deferred(),
|
||||
timeout: 250
|
||||
});
|
||||
|
||||
```
|
||||
|
||||
Currently this plugin supports the following options:
|
||||
|
||||
####globalStyles
|
||||
@@ -79,6 +89,13 @@ Currently this plugin supports the following options:
|
||||
- Acceptable-Values: Any valid `jQuery.Deferred` object
|
||||
- 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
|
||||
|
||||
### jQuery
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "jQuery.print",
|
||||
"main": "jQuery.print.js",
|
||||
"version": "1.3.0",
|
||||
"version": "1.3.3",
|
||||
"homepage": "https://doersguild.github.io/jQuery.print/",
|
||||
"authors": [
|
||||
"Sathvik P <sathvik@doersguild.com>"
|
||||
@@ -23,4 +23,4 @@
|
||||
"test",
|
||||
"tests"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
+5
-5
@@ -98,10 +98,10 @@
|
||||
iframe : false,
|
||||
//Don't print this
|
||||
noPrintSelector : ".avoid-this",
|
||||
//Add this on top
|
||||
append : "Hello World!!!<br/>",
|
||||
//Add this at bottom
|
||||
prepend : "<br/>Buh Bye!"
|
||||
//Add this at top
|
||||
prepend : "Hello World!!!<br/>",
|
||||
//Add this on bottom
|
||||
append : "<br/>Buh Bye!"
|
||||
});
|
||||
});
|
||||
// Fork https://github.com/sathvikp/jQuery.print for the full list of options
|
||||
@@ -109,4 +109,4 @@
|
||||
//]]>
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
||||
+25
-18
@@ -1,5 +1,5 @@
|
||||
/* @license
|
||||
* jQuery.print, version 1.3.0
|
||||
* jQuery.print, version 1.3.3
|
||||
* (c) Sathvik Ponangi, Doers' Guild
|
||||
* Licence: CC-By (http://creativecommons.org/licenses/by/3.0/)
|
||||
*--------------------------------------------------------------------------*/
|
||||
@@ -19,7 +19,7 @@
|
||||
return jqObj;
|
||||
}
|
||||
|
||||
function printFrame(frameWindow) {
|
||||
function printFrame(frameWindow, timeout) {
|
||||
// Print the selected window/iframe
|
||||
var def = $.Deferred();
|
||||
try {
|
||||
@@ -36,20 +36,20 @@
|
||||
frameWindow.print();
|
||||
}
|
||||
frameWindow.close();
|
||||
deferred.resolve();
|
||||
}, 250);
|
||||
def.resolve();
|
||||
}, timeout);
|
||||
} catch (err) {
|
||||
def.reject(err);
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
function printContentInNewWindow(content) {
|
||||
function printContentInNewWindow(content, timeout) {
|
||||
// Open a new window and print selected content
|
||||
var w = window.open();
|
||||
w.document.write(content);
|
||||
w.document.close();
|
||||
return printFrame(w);
|
||||
return printFrame(w, timeout);
|
||||
}
|
||||
|
||||
function isNode(o) {
|
||||
@@ -99,7 +99,8 @@
|
||||
append: null,
|
||||
prepend: null,
|
||||
manuallyCopyFormValues: true,
|
||||
deferred: $.Deferred()
|
||||
deferred: $.Deferred(),
|
||||
timeout: 250
|
||||
};
|
||||
// Merge with user-options
|
||||
options = $.extend({}, defaults, (options || {}));
|
||||
@@ -132,20 +133,26 @@
|
||||
if (options.manuallyCopyFormValues) {
|
||||
// Manually copy form values into the HTML for printing user-modified input fields
|
||||
// http://stackoverflow.com/a/26707753
|
||||
copy.find("input, select, textarea")
|
||||
copy.find("input")
|
||||
.each(function () {
|
||||
var $field = $(this);
|
||||
if ($field.is("[type='radio']") || $field.is("[type='checkbox']")) {
|
||||
if ($field.prop("checked")) {
|
||||
$field.attr("checked", "checked");
|
||||
}
|
||||
} else if ($field.is("select")) {
|
||||
$field.find(":selected")
|
||||
.attr("selected", "selected");
|
||||
} else {
|
||||
$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
|
||||
var content = copy.html();
|
||||
@@ -179,7 +186,7 @@
|
||||
wdoc.open();
|
||||
wdoc.write(content);
|
||||
wdoc.close();
|
||||
printFrame(w)
|
||||
printFrame(w, options.timeout)
|
||||
.done(function () {
|
||||
// Success
|
||||
setTimeout(function () {
|
||||
@@ -190,10 +197,10 @@
|
||||
}
|
||||
}, 100);
|
||||
})
|
||||
.fail(function () {
|
||||
.fail(function (err) {
|
||||
// Use the pop-up method if iframe fails for some reason
|
||||
console.error("Failed to print from iframe", e.stack, e.message);
|
||||
printContentInNewWindow(content);
|
||||
console.error("Failed to print from iframe", err);
|
||||
printContentInNewWindow(content, options.timeout);
|
||||
})
|
||||
.always(function () {
|
||||
try {
|
||||
@@ -205,7 +212,7 @@
|
||||
} catch (e) {
|
||||
// Use the pop-up method if iframe fails for some reason
|
||||
console.error("Failed to print from iframe", e.stack, e.message);
|
||||
printContentInNewWindow(content)
|
||||
printContentInNewWindow(content, options.timeout)
|
||||
.always(function () {
|
||||
try {
|
||||
options.deferred.resolve();
|
||||
@@ -216,7 +223,7 @@
|
||||
}
|
||||
} else {
|
||||
// Use a new window for printing
|
||||
printContentInNewWindow(content)
|
||||
printContentInNewWindow(content, options.timeout)
|
||||
.always(function () {
|
||||
try {
|
||||
options.deferred.resolve();
|
||||
@@ -227,4 +234,4 @@
|
||||
}
|
||||
return this;
|
||||
};
|
||||
})(jQuery);
|
||||
})(jQuery);
|
||||
|
||||
Reference in New Issue
Block a user