Skip to content

Instantly share code, notes, and snippets.

@davetapley
Created February 1, 2016 19:31
Show Gist options
  • 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