window.print()对话框未在Chrome中显示,它在IE,Firefox和Safari中正常工作

时间:2014-07-28 07:03:17

标签: javascript google-chrome printing

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浏览器中提示对话框。 已经经历了许多事情,但无法提供解决方案。

1 个答案:

答案 0 :(得分:0)

最后,它适用于我。

这是我的代码

 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.
                    }