Skip to content

Commit

Permalink
Reorganized code and added the doctype option for #36
Browse files Browse the repository at this point in the history
  • Loading branch information
Sathvik P committed Feb 10, 2016
1 parent aabf7f4 commit bb15701
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 115 deletions.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ You can submit the options object like:
manuallyCopyFormValues: true,
deferred: $.Deferred(),
timeout: 250,
title: null
title: null,
doctype: '<!doctype html>'
});
```
Expand Down Expand Up @@ -101,16 +102,24 @@ Currently this plugin supports the following options:
- Default: `null`, uses the host page title
- Acceptable-Values: Any single-line string
- Function: To change the printed title
####doctype
- Default: `'<!doctype html>'`
- Acceptable-Values: Any valid doctype string
- Function: To prepend a doctype to the printed document frame
## Tested with
### jQuery
* [jQuery](http://jquery.com/) v. 1.7.2
* [jQuery](http://jquery.com/) v. 1.9.1
* [jQuery](http://jquery.com/) v. 2.2.0
### Browsers
* Google Chrome - v 20, 26
* Internet Explorer - v 10
* Google Chrome - v 20, 26, 48
* Internet Explorer - v 10, 11
* Firefox - v 35
## License
[CC-BY](http://creativecommons.org/licenses/by/3.0/).
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jQuery.print",
"main": "jQuery.print.js",
"version": "1.3.3",
"version": "1.4.0",
"homepage": "https://doersguild.github.io/jQuery.print/",
"authors": [
"Sathvik P <[email protected]>"
Expand Down
76 changes: 38 additions & 38 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="css/normalize.min.css">
<style type='text/css'>
.a {
background: black;
color: white;
}
.b {
color: #aaa;
}
.a {
background: black;
color: white;
}
.b {
color: #aaa;
}
</style>
<!--[if lt IE 9]>
<script src="js/vendor/html5-3.6-respond-1.1.0.min.js"></script>
<![endif]-->
</head>
<body>
<!--[if lt IE 9]>
<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>
<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>
<![endif]-->
<h3><a href="https://github.com/DoersGuild/jQuery.print" id="view-on-github" class="btn"><span>View jQuery.print on GitHub</span></a></h3>
<div id="content_holder">
Expand Down Expand Up @@ -73,40 +73,40 @@ <h3 class='avoid-this'>Element 4</h3>
Print page
</button>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script>
window.jQuery || document.write('<script src="js/vendor/jquery-1.9.1.min.js"><\/script>')
window.jQuery || document.write('<script src="../bower_components/jquery/dist/jquery.min.js"><\/script>')
</script>
<script src="../jQuery.print.js"></script>
<script type='text/javascript'>
//<![CDATA[
$(function() {
$("#ele2").find('.print-link').on('click', function() {
//Print ele2 with default options
$.print("#ele2");
});
$("#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!!!<br/>",
//Add this on bottom
append : "<br/>Buh Bye!"
});
});
// Fork https://github.com/sathvikp/jQuery.print for the full list of options
});
//]]>
//<![CDATA[
$(function() {
$("#ele2").find('.print-link').on('click', function() {
//Print ele2 with default options
$.print("#ele2");
});
$("#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!!!<br/>",
//Add this on bottom
append : "<br/>Buh Bye!"
});
});
// Fork https://github.com/sathvikp/jQuery.print for the full list of options
});
//]]>
</script>
</body>
</html>
5 changes: 0 additions & 5 deletions demo/js/vendor/jquery-1.9.1.min.js

This file was deleted.

131 changes: 64 additions & 67 deletions jQuery.print.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* @license
* jQuery.print, version 1.3.3
* jQuery.print, version 1.4.0
* (c) Sathvik Ponangi, Doers' Guild
* Licence: CC-By (http://creativecommons.org/licenses/by/3.0/)
*--------------------------------------------------------------------------*/
Expand All @@ -19,10 +19,17 @@
return jqObj;
}

function printFrame(frameWindow, timeout) {
function printFrame(frameWindow, content, options) {
// Print the selected window/iframe
var def = $.Deferred();
try {
frameWindow = frameWindow.contentWindow || frameWindow.contentDocument || frameWindow;
var wdoc = frameWindow.document || frameWindow.contentDocument || frameWindow;
if(options.doctype) {
wdoc.write(options.doctype);
}
wdoc.write(content);
wdoc.close();
setTimeout(function () {
// Fix for IE : Allow it to render the iframe
frameWindow.focus();
Expand All @@ -37,19 +44,63 @@
}
frameWindow.close();
def.resolve();
}, timeout);
}, options.timeout);
} catch (err) {
def.reject(err);
}
return def;
}

function printContentInNewWindow(content, timeout) {
function printContentInIFrame(content, options) {
var $iframe = $(options.iframe + "");
var iframeCount = $iframe.length;
if (iframeCount === 0) {
// Create a new iFrame if none is given
$iframe = $('<iframe height="0" width="0" border="0" wmode="Opaque"/>')
.prependTo('body')
.css({
"position": "absolute",
"top": -999,
"left": -999
});
}
var frameWindow = $iframe.get(0);
return printFrame(frameWindow, content, options)
.done(function () {
// Success
setTimeout(function () {
// Wait for IE
if (iframeCount === 0) {
// Destroy the iframe if created here
$iframe.remove();
}
}, 100);
})
.fail(function (err) {
// Use the pop-up method if iframe fails for some reason
console.error("Failed to print from iframe", err);
printContentInNewWindow(content, options);
})
.always(function () {
try {
options.deferred.resolve();
} catch (err) {
console.warn('Error notifying deferred', err);
}
});
}

function printContentInNewWindow(content, options) {
// Open a new window and print selected content
var w = window.open();
w.document.write(content);
w.document.close();
return printFrame(w, timeout);
var frameWindow = window.open();
return printFrame(frameWindow, content, options)
.always(function () {
try {
options.deferred.resolve();
} catch (err) {
console.warn('Error notifying deferred', err);
}
});
}

function isNode(o) {
Expand Down Expand Up @@ -101,7 +152,8 @@
manuallyCopyFormValues: true,
deferred: $.Deferred(),
timeout: 250,
title: null
title: null,
doctype: '<!doctype html>'
};
// Merge with user-options
options = $.extend({}, defaults, (options || {}));
Expand Down Expand Up @@ -177,70 +229,15 @@
if (options.iframe) {
// Use an iframe for printing
try {
var $iframe = $(options.iframe + "");
var iframeCount = $iframe.length;
if (iframeCount === 0) {
// Create a new iFrame if none is given
$iframe = $('<iframe height="0" width="0" border="0" wmode="Opaque"/>')
.prependTo('body')
.css({
"position": "absolute",
"top": -999,
"left": -999
});
}
var w, wdoc;
w = $iframe.get(0);
w = w.contentWindow || w.contentDocument || w;
wdoc = w.document || w.contentDocument || w;
wdoc.open();
wdoc.write(content);
wdoc.close();
printFrame(w, options.timeout)
.done(function () {
// Success
setTimeout(function () {
// Wait for IE
if (iframeCount === 0) {
// Destroy the iframe if created here
$iframe.remove();
}
}, 100);
})
.fail(function (err) {
// Use the pop-up method if iframe fails for some reason
console.error("Failed to print from iframe", err);
printContentInNewWindow(content, options.timeout);
})
.always(function () {
try {
options.deferred.resolve();
} catch (err) {
console.warn('Error notifying deferred', err);
}
});
printContentInIFrame(content, options);
} 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, options.timeout)
.always(function () {
try {
options.deferred.resolve();
} catch (err) {
console.warn('Error notifying deferred', err);
}
});
printContentInNewWindow(content, options);
}
} else {
// Use a new window for printing
printContentInNewWindow(content, options.timeout)
.always(function () {
try {
options.deferred.resolve();
} catch (err) {
console.warn('Error notifying deferred', err);
}
});
printContentInNewWindow(content, options);
}
return this;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jQuery.print",
"filename": "jQuery.print.min.js",
"version": "1.3.3",
"version": "1.4.0",
"homepage": "https://doersguild.github.io/jQuery.print/",
"authors": [
"Sathvik P <[email protected]>"
Expand Down

0 comments on commit bb15701

Please sign in to comment.