Skip to content

Commit bb15701

Browse files
author
Sathvik P
committed
Reorganized code and added the doctype option for #36
1 parent aabf7f4 commit bb15701

File tree

6 files changed

+116
-115
lines changed

6 files changed

+116
-115
lines changed

README.md

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ You can submit the options object like:
3636
manuallyCopyFormValues: true,
3737
deferred: $.Deferred(),
3838
timeout: 250,
39-
title: null
39+
title: null,
40+
doctype: '<!doctype html>'
4041
});
4142
```
4243
@@ -101,16 +102,24 @@ Currently this plugin supports the following options:
101102
- Default: `null`, uses the host page title
102103
- Acceptable-Values: Any single-line string
103104
- Function: To change the printed title
105+
106+
####doctype
107+
108+
- Default: `'<!doctype html>'`
109+
- Acceptable-Values: Any valid doctype string
110+
- Function: To prepend a doctype to the printed document frame
104111
105112
## Tested with
106113
107114
### jQuery
108115
* [jQuery](http://jquery.com/) v. 1.7.2
109116
* [jQuery](http://jquery.com/) v. 1.9.1
117+
* [jQuery](http://jquery.com/) v. 2.2.0
110118
111119
### Browsers
112-
* Google Chrome - v 20, 26
113-
* Internet Explorer - v 10
120+
* Google Chrome - v 20, 26, 48
121+
* Internet Explorer - v 10, 11
122+
* Firefox - v 35
114123
115124
## License
116125
[CC-BY](http://creativecommons.org/licenses/by/3.0/).

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jQuery.print",
33
"main": "jQuery.print.js",
4-
"version": "1.3.3",
4+
"version": "1.4.0",
55
"homepage": "https://doersguild.github.io/jQuery.print/",
66
"authors": [
77
"Sathvik P <[email protected]>"

demo/index.html

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@
1313
<meta name="viewport" content="width=device-width">
1414
<link rel="stylesheet" href="css/normalize.min.css">
1515
<style type='text/css'>
16-
.a {
17-
background: black;
18-
color: white;
19-
}
20-
.b {
21-
color: #aaa;
22-
}
16+
.a {
17+
background: black;
18+
color: white;
19+
}
20+
.b {
21+
color: #aaa;
22+
}
2323
</style>
2424
<!--[if lt IE 9]>
2525
<script src="js/vendor/html5-3.6-respond-1.1.0.min.js"></script>
2626
<![endif]-->
2727
</head>
2828
<body>
2929
<!--[if lt IE 9]>
30-
<p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
30+
<p class="chromeframe">You are using an <strong>outdated</strong> browser. Please <a href="http://browsehappy.com/">upgrade your browser</a> or <a href="http://www.google.com/chromeframe/?redirect=true">activate Google Chrome Frame</a> to improve your experience.</p>
3131
<![endif]-->
3232
<h3><a href="https://github.com/DoersGuild/jQuery.print" id="view-on-github" class="btn"><span>View jQuery.print on GitHub</span></a></h3>
3333
<div id="content_holder">
@@ -73,40 +73,40 @@ <h3 class='avoid-this'>Element 4</h3>
7373
Print page
7474
</button>
7575
</div>
76-
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
76+
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
7777
<script>
78-
window.jQuery || document.write('<script src="js/vendor/jquery-1.9.1.min.js"><\/script>')
78+
window.jQuery || document.write('<script src="../bower_components/jquery/dist/jquery.min.js"><\/script>')
7979
</script>
8080
<script src="../jQuery.print.js"></script>
8181
<script type='text/javascript'>
82-
//<![CDATA[
83-
$(function() {
84-
$("#ele2").find('.print-link').on('click', function() {
85-
//Print ele2 with default options
86-
$.print("#ele2");
87-
});
88-
$("#ele4").find('button').on('click', function() {
89-
//Print ele4 with custom options
90-
$("#ele4").print({
91-
//Use Global styles
92-
globalStyles : false,
93-
//Add link with attrbute media=print
94-
mediaPrint : false,
95-
//Custom stylesheet
96-
stylesheet : "http://fonts.googleapis.com/css?family=Inconsolata",
97-
//Print in a hidden iframe
98-
iframe : false,
99-
//Don't print this
100-
noPrintSelector : ".avoid-this",
101-
//Add this at top
102-
prepend : "Hello World!!!<br/>",
103-
//Add this on bottom
104-
append : "<br/>Buh Bye!"
105-
});
106-
});
107-
// Fork https://github.com/sathvikp/jQuery.print for the full list of options
108-
});
109-
//]]>
82+
//<![CDATA[
83+
$(function() {
84+
$("#ele2").find('.print-link').on('click', function() {
85+
//Print ele2 with default options
86+
$.print("#ele2");
87+
});
88+
$("#ele4").find('button').on('click', function() {
89+
//Print ele4 with custom options
90+
$("#ele4").print({
91+
//Use Global styles
92+
globalStyles : false,
93+
//Add link with attrbute media=print
94+
mediaPrint : false,
95+
//Custom stylesheet
96+
stylesheet : "http://fonts.googleapis.com/css?family=Inconsolata",
97+
//Print in a hidden iframe
98+
iframe : false,
99+
//Don't print this
100+
noPrintSelector : ".avoid-this",
101+
//Add this at top
102+
prepend : "Hello World!!!<br/>",
103+
//Add this on bottom
104+
append : "<br/>Buh Bye!"
105+
});
106+
});
107+
// Fork https://github.com/sathvikp/jQuery.print for the full list of options
108+
});
109+
//]]>
110110
</script>
111111
</body>
112112
</html>

demo/js/vendor/jquery-1.9.1.min.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

jQuery.print.js

Lines changed: 64 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* @license
2-
* jQuery.print, version 1.3.3
2+
* jQuery.print, version 1.4.0
33
* (c) Sathvik Ponangi, Doers' Guild
44
* Licence: CC-By (http://creativecommons.org/licenses/by/3.0/)
55
*--------------------------------------------------------------------------*/
@@ -19,10 +19,17 @@
1919
return jqObj;
2020
}
2121

22-
function printFrame(frameWindow, timeout) {
22+
function printFrame(frameWindow, content, options) {
2323
// Print the selected window/iframe
2424
var def = $.Deferred();
2525
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();
2633
setTimeout(function () {
2734
// Fix for IE : Allow it to render the iframe
2835
frameWindow.focus();
@@ -37,19 +44,63 @@
3744
}
3845
frameWindow.close();
3946
def.resolve();
40-
}, timeout);
47+
}, options.timeout);
4148
} catch (err) {
4249
def.reject(err);
4350
}
4451
return def;
4552
}
4653

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) {
4894
// 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+
});
53104
}
54105

55106
function isNode(o) {
@@ -101,7 +152,8 @@
101152
manuallyCopyFormValues: true,
102153
deferred: $.Deferred(),
103154
timeout: 250,
104-
title: null
155+
title: null,
156+
doctype: '<!doctype html>'
105157
};
106158
// Merge with user-options
107159
options = $.extend({}, defaults, (options || {}));
@@ -177,70 +229,15 @@
177229
if (options.iframe) {
178230
// Use an iframe for printing
179231
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);
222233
} catch (e) {
223234
// Use the pop-up method if iframe fails for some reason
224235
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);
233237
}
234238
} else {
235239
// 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);
244241
}
245242
return this;
246243
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "jQuery.print",
33
"filename": "jQuery.print.min.js",
4-
"version": "1.3.3",
4+
"version": "1.4.0",
55
"homepage": "https://doersguild.github.io/jQuery.print/",
66
"authors": [
77
"Sathvik P <[email protected]>"

0 commit comments

Comments
 (0)