Skip to content

Instantly share code, notes, and snippets.

@wpscholar
Created October 18, 2017 21:15
Show Gist options
  • Save wpscholar/acedc387110a1a3389f802372acfbf57 to your computer and use it in GitHub Desktop.
Save wpscholar/acedc387110a1a3389f802372acfbf57 to your computer and use it in GitHub Desktop.
Print part of the screen based on a CSS selector
function print(selector) {
var w = window.open('', '', 'left=0,top=0,width=' + window.width + ',height=' + window.height + ',toolbar=0,scrollbars=0,status=0');
w.document.write('<link rel="stylesheet" type="text/css" href="/path/to.css" />');
w.document.write(document.querySelector(selector).innerHTML);
w.document.close();
w.addEventListener('load', function () {
w.focus();
w.print();
w.close();
}, true);
}
@wpscholar
Copy link
Author

Probably should update this to automatically pull in all the stylesheets from the existing page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment