[英]How to redirect a user to another page with a button click using only JavaScript?
[英]How to redirect to another page ONLY if the user prints
如果用户实际打印,我一直试图找到一种重定向到另一个页面的方法。 也就是说,只有当他们从打印时打开的选项卡中单击该打印按钮时,我才想重定向。
我不想要这种方法。
function myFunction() {
window.print();
window.location='http://newurl.com/';
}
摘自https://www.tjvantoll.com/2012/06/15/detecting-print-requests-with-javascript/下面:
组合方法 如果您将这两种方法组合在一起,您可以在 IE 5+、Firefox 6+、Chrome 9+ 和 Safari 5.1+ 中检测打印请求(不幸的是 Opera 不支持任何一种方法)。
(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;
}());
请注意,您的事件处理程序可能必须处理这样一个事实,即在 Chrome 中每个打印请求将调用它们两次。
节选结束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.