Created
February 1, 2016 19:31
-
-
Save davetapley/289746e4cc6f26e3e3d2 to your computer and use it in GitHub Desktop.
Custom print DOM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@media print { | |
body>:not(.printme) { | |
display: none; | |
} | |
} | |
@media not print{ | |
.printme { | |
display: none; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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