确定是否单击了Google Chrome打印预览中的打印/取消按钮 [英] Determine If Print/Cancel Button in Google Chrome's Print Preview is Clicked
问题描述
我一直在使用以下代码打印我的页面:
I've been printing my page using the code below:
window.print();
下面的图片是Google Chrome浏览器中的打印预览。它有两个主要按钮: print
和取消
。
An image below is what the print preview in Google chrome browser looks like. It has two main buttons: print
and cancel
.
我想知道如果用户点击了打印
或取消
按钮。我做了什么使用jquery:
I want to know if the user has clicked the print
or cancel
buttons. What I did uses jquery:
打印预览的HTML代码:
HTML Code of the Print Preview:
<button class="print default" i18n-content="printButton">Print</button>
<button class="cancel" i18n-content="cancel">Cancel</button>
Jquery代码:
$('button > .cancel').click(function (e) {
alert('Cancel');
});
$('button > .print').click(function (e) {
alert('Print');
});
我试过上面的代码没有运气。我缺少什么?
I tried the code above with no luck. What am I missing?
推荐答案
您无法直接从常规网页访问Chrome的内部窗口(本例中为打印对话框) 。
You can not access Chrome's internal windows (printing dialog in this case) directly from a regular web page.
(function () {
var beforePrint = function () {
alert('Functionality to run before printing.');
};
var afterPrint = function () {
alert('Functionality to run after printing');
};
if (window.matchMedia) {
var mediaQueryList = window.matchMedia('print');
mediaQueryList.addListener(function (mql) {
//alert($(mediaQueryList).html());
if (mql.matches) {
beforePrint();
} else {
afterPrint();
}
});
}
window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;
}());
或者,如果您想在打印预览打开时执行某些操作,可以尝试以下操作:
Or, If you want to do something when the print preview gets opened, you can try below:
$(document).bind("keyup keydown", function (e) {
if (e.ctrlKey && e.keyCode == 80) {
setTimeout(function () { CallAfterWindowLoad();}, 5000);
return true;
}
});
function CallAfterWindowLoad()
{
alert("Open and call");
}
参考:
如何在默认打印菜单上捕获点击事件通过Javascript window.print()
如果您提供了这两个按钮点击事件的要求,我们可以为您提供备用解决方案。
Maybe if you provide your requirements for this two buttons click event, we can provide you an alternate solution.
这篇关于确定是否单击了Google Chrome打印预览中的打印/取消按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!