Microsoft Edge浏览器和打印

时间:2015-08-24 13:21:24

标签: javascript jquery printing microsoft-edge

我有一个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);
});
}

2 个答案:

答案 0 :(得分:1)

这是一个问题,已发布在MS Connect网站[Edge] Printing iframe window ends up printing top window

答案 1 :(得分:0)

我们找到了这个解决方案:

parent.document.getElementsByName("pdfjs-frame")[0].contentWindow.document.execCommand("print", false, null);

posted here on StackOverflow

相关问题