Skip to content

Instantly share code, notes, and snippets.

@davetapley
Created February 1, 2016 19:31
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 davetapley/289746e4cc6f26e3e3d2 to your computer and use it in GitHub Desktop.
Save davetapley/289746e4cc6f26e3e3d2 to your computer and use it in GitHub Desktop.
Custom print DOM
@media print {
body>:not(.printme) {
display: none;
}
}
@media not print{
.printme {
display: none;
}
}
(function() {
var beforePrint = function(e) {
$('body').append('<div class="printme"><p>HELLO PRINTER</p></div>');
};
var afterPrint = function() {
console.log('Functionality to run after printing');
};
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if (mql.matches) {
beforePrint();
} else {
afterPrint();
}
});
}
window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;
}())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment