Skip to content

Instantly share code, notes, and snippets.

@eusonlito
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eusonlito/9730e60a575c626f4935 to your computer and use it in GitHub Desktop.
Save eusonlito/9730e60a575c626f4935 to your computer and use it in GitHub Desktop.
// Usage <button type="button" data-print=".section-to-print">Print</button>
(function ($) {
'use strict';
$('[data-print]').on('click', function (e) {
e.preventDefault();
var $this = $(this),
$print = $($this.data('print'));
if ($print.length === 0) {
return;
}
var $head = $('head').clone();
$head.find('script').remove();
var win = window.open();
win.focus();
win.document.open();
win.document.write('<html><head>' + $head.html() + '</head>');
win.document.write('<body style="background: none;">' + $print.html() + '</body></html>');
win.document.close();
win.onload = function() {
win.print();
win.close();
};
});
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment