I have a page that calls window.print(); at the bottom of the page. I have no way of accessing the code around window.print(); Its generated by the server and I can\'t tou
This function works for all browsers including IE, no matter how the print is called:
/**
* Adds listeners for crossbrowser print callback
* @param callback - callback function
*/
function onPrint(callback) {
window.matchMedia('print').addListener(query => query.matches ? callback() : null)
window.addEventListener('beforeprint', () => callback())
}
onPrint(() => console.log('printing!'))
Be aware it may run the callback function twice, depending on the browser. This could be circumvented with a temporary flag.