【问题标题】:window.print() dialog not showing in Chrome wher it works fine in IE , Firefox and Safariwindow.print() 对话框未在 Chrome 中显示,但在 IE、Firefox 和 Safari 中运行良好
【发布时间】:2014-07-28 07:03:17
【问题描述】:

Chrome 中没有显示打印对话框

 var newWin = window.open("","",'width=0,height=0');
        $.ajax({
            type: "POST",
            async: true,
            url: '/BarcodePrintTest/PrintBarcodeLabel1',
            data: JSON.stringify(dataToSend),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                if (data != "") {
                    newWin.document.write('</head><body >');
                    newWin.document.write(JSON.stringify(data.replace(/\r\n/g, "<br/>")));
                    newWin.document.write('</body></html>');
                    newWin.document.close();
                    newWin.resizeTo(0, 0);
                    newWin.moveTo(0, window.screen.availHeight + 10);
                    newWin.focus();
                    newWin.print();
                   // setTimeout(function () { newWin.close(); }, 1);
                    newWin.close();
                    return false;
                   // window.location = '@Url.Action("Create", "BarcodePrintTest")';
                }
                newWin.close();
                return false;
            },
            error: function () {
                alert('Error in print');
            }

这是我的代码... 它在 Firefox、IE 和 Safari 中完美地提示打印对话框,但在 chrome 浏览器中没有提示对话框。 历经千辛万苦,却未能提出解决方案。

【问题讨论】:

  • SO 中最常见的问题,在 browser 中不起作用;)
  • 是的..我们有任何解决方案
  • 另见this
  • @NavinRauniyar 可能是我的回答对你的帖子有帮助

标签: javascript google-chrome printing


【解决方案1】:

最后它在 chrome 中对我有用。

这是我的代码

 var newWin = window.open("","",'width=0,height=0');
        $.ajax({
            type: "POST",
            async: true,
            url: '/BarcodePrintTest/PrintBarcodeLabel1',
            data: JSON.stringify(dataToSend),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                if (data != "") {
                    debugger
                    if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {   // Chrome Browser Detected?
                        newWin.PPClose = false;                                     // Clear Close Flag
                        newWin.onbeforeunload = function () {                         // Before Window Close Event
                            if (newWin.PPClose === false) {                           // Close not OK?
                                return 'Leaving this page will block the parent window!\nPlease select "Stay on this Page option" and use the\nCancel button instead to close the Print Preview Window.\n';
                            }
                        }
                        newWin.document.write('</head><body >');
                        newWin.document.write(JSON.stringify(data.replace(/\r\n/g, "<br/>")));
                        newWin.document.write('</body></html>');
                        newWin.document.close();
                       // newWin.resizeTo(0, 0);
                        //newWin.moveTo(0, window.screen.availHeight + 10);
                       // newWin.focus();
                        newWin.print();                                             // Print preview
                        newWin.close();
                        newWin.PPClose = true;                                      // Set Close Flag to OK.
                    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-25
    • 2013-03-29
    • 2014-09-26
    • 2013-09-27
    • 2016-10-03
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多