0

Print dialog is not showing in 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');
            }

This is my code ... It is prompting print dialoge in Firefox,IE and Safari perfectly but didn't promt the dialog in chrome browser. Have gone through many threds but unable to produce solution.

4
  • most common question in SO, not working in this browser ;) Jul 28, 2014 at 7:04
  • yup..do we have any solution for this
    – Karthik
    Jul 28, 2014 at 7:05
  • Also, See this Jul 28, 2014 at 7:06
  • @NavinRauniyar may be my answer will be helpful to ur post
    – Karthik
    Jul 28, 2014 at 7:34

1 Answer 1

0

Finally it works for me in chrome.

Here is my code

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.