Skip to content

Instantly share code, notes, and snippets.

@Trshant
Created March 22, 2016 08:30
Show Gist options
  • Save Trshant/80962af477bb0b2f19dc to your computer and use it in GitHub Desktop.
Save Trshant/80962af477bb0b2f19dc to your computer and use it in GitHub Desktop.
Detecting 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