JavaScript print blocked by Chrome, Workaround? JavaScript print blocked by Chrome, Workaround? google-chrome google-chrome

JavaScript print blocked by Chrome, Workaround?


You could conditionally replace the window.print() function:

// detect if browser is Chromeif(navigator.userAgent.toLowerCase().indexOf("chrome") >  -1) {    // wrap private vars in a closure    (function() {        var realPrintFunc = window.print;        var interval = 2500; // 2.5 secs        var nextAvailableTime = +new Date(); // when we can safely print again        // overwrite window.print function        window.print = function() {            var now = +new Date();            // if the next available time is in the past, print now            if(now > nextAvailableTime) {                realPrintFunc();                nextAvailableTime = now + interval;            } else {                // print when next available                setTimeout(realPrintFunc, nextAvailableTime - now);                nextAvailableTime += interval;            }        }    })();}

Instead of using an external safety timer/wrapper, you can use an internal one. Just add this and window.print behaves safely in Chrome and normally everywhere else. (Plus, the closure means realPrintFunc, interval and nextAvailableTime are private to the new window.print

If you need to coordinate calls to window.print between multiple framed pages, you could set nextAvailableTime in the parent page, rather than in the closure, so that all the frames could access a shared value using window.parent.nextAvailableTime.


I've been bumping up against the same issue, and the most direct solution for me was to just create a new window, write what I need to it, close it, and print it. I haven't had to deal with Chrome's limit since I changed it to work this way, and I don't need to do any tracking.

print_window= window.open();print_window.document.write(print_css + divToPrint[0].outerHTML+"</html>");print_window.document.close();print_window.focus();print_window.print();print_window.close();


My solution would be to call the window.print() less frequently. You could try wrapping window.print() into a method of your own and put a minimum time interval that has to pass before the window.print() is executed again. You can have some kind of change/animation in UI to indicate that work is being done.

"Working"

Unless you think it really think pressing print-button more than once a second/every few seconds helps? I mean if the window.print() operation takes that long it's not your codes fault and there should be a "wait and be patient" -indicator in UI anyway.