Skip to content

Instantly share code, notes, and snippets.

@dperrymorrow
Created July 22, 2014 20:53
Show Gist options
  • Save dperrymorrow/8cea669cd50f6f6dd74b to your computer and use it in GitHub Desktop.
Save dperrymorrow/8cea669cd50f6f6dd74b to your computer and use it in GitHub Desktop.
catching print requests with javascript
(function() {
var beforePrint = function() {
console.log('Functionality to run before printing.');
};
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