【发布时间】:2015-08-24 13:21:23
【问题描述】:
我有一个 printUrl javascript/jquery 函数,可以将我的网页的打印友好版本加载到 iFrame 中并打印出来。它似乎在 Chrome、Firefox 和 IE 中工作,但我无法在 Microsoft Edge 浏览器中工作。打印对话框出现,但消息“没有发送到打印”为红色。任何帮助,将不胜感激。函数如下:
function printUrl(url) {
$('body').append('<iframe width="1" height="1" id="printFrame" style="display: none; @media print { display: block; }"/>');
$('#printFrame').attr('src', url);
$('#printFrame').load(function() {
var ua = window.navigator.userAgent;
var msie = ua.indexOf("Trident"); // detect if IE
if (msie > 0) {
var target = document.getElementById('printFrame');
try {
target.contentWindow.document.execCommand('print', false, null);
} catch (e) {
target.contentWindow.print();
}
} else {
// this code executes for Edge printing as well as Chrome, Firefox
var frame = document.getElementById('printFrame');
if (!frame) {
$.alert("Error: Can't find printing frame.");
return;
}
frame = frame.contentWindow;
frame.focus();
frame.print();
}
setTimeout(function() {
$('#printFrame').remove()
}, 500);
});
}
【问题讨论】:
标签: javascript jquery printing microsoft-edge