'window.print() doesn't work on mobile browswer

I'm using this function to open a new tab with the html content take from another file and then print it. When I use it on a descktop browser it works fine. It opens a new tab with the content give and prints the page but when I try to do it from my mobile device the tab opens with its content and then I get the printer popup window but it says "There was a problem printing the page. Please try again." I've tried to press retry but it doesn't do anything.

Here's the function.

    function printit() {
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", "/Doc/New Text Document.html", false);
    rawFile.onreadystatechange = function () {
        if (rawFile.readyState === 4) {
            if (rawFile.status === 200 || rawFile.status == 0) {
                var allText = rawFile.responseText;
                var myWindow = window.open('about:blank', '_blank');
                myWindow.document.write(allText);

                myWindow.document.close();
                myWindow.focus();
                myWindow.print();
                myWindow.close();
            }
        }
    }
    rawFile.send(null);
}


Solution 1:[1]

I had to use a setTimeout() because the page would close before the printing was done and the printing service couldnt find the page content.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shatsuki