Skip to content

Instantly share code, notes, and snippets.

@examinedliving
Created April 11, 2017 14:49
Show Gist options
  • Select an option

  • Save examinedliving/b9218ee78587be5090101315da015198 to your computer and use it in GitHub Desktop.

Select an option

Save examinedliving/b9218ee78587be5090101315da015198 to your computer and use it in GitHub Desktop.
Print from iFrame in current window, and add callbacks for before print, and after print.
(function() {
var beforePrint = function() {
console.log('Functionality to run before printing.');
};
var afterPrint = function() {
console.log('Functionality to run after printing');
};
var ifr=''//iframe ref;
ifr.focus();
var cw=''//ifr.contentWindow
if (window.matchMedia) {
var mediaQueryList = cw.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if (mql.matches) {
beforePrint();
} else {
afterPrint();
}
});
}
cw.onbeforeprint = beforePrint;
cw.onafterprint = afterPrint;
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment