情况1)在IE8上,回调前函数可以,回调后函数
alert方式测试:
测试结果:
顺序1:alert(1)
顺序2:alert(2)
顺序3:弹出打印界面
情况2)谷歌上调用,回调前,回调后,两个函数,会各自调用两次
alert方式测试:
测试结果:
顺序1:alert(1)
顺序2:弹出打印界面
顺序3:alert(1)
顺序4:alert(2)
顺序5:alert(2)
console.log方式测试:
测试结果:beforePrint,beforePrint,afterPrint,afterPrint
函数:
$(function(){
var beforePrint = function() {
console.log("beforePrint");
alert(1);
};
var afterPrint = function() {
console.log("afterPrint");
alert(2);
};
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function(mql) {
if (mql.matches) {
beforePrint();
} else {
afterPrint();
}
});
}
window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;
});
function applyPrint(){
window.print();
}
参考:
segmentfault:调用window.print()方法,如何获取打印或者取消的状态?