我有一個printUrl javascript/jquery函數,它將我的網頁的打印友好版本加載到iFrame中並打印出來。它似乎可以在Chrome,Firefox和IE中使用,但我無法在Microsoft Edge瀏覽器中使用它。打印對話框出現,但顯示紅色的消息「無法打印」。任何幫助,將不勝感激。以下功能: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);
});
}