2

Below is my piece of code that work's in all the browser in all the OS, except ipad chrome. Help me out here.

<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <meta http-equiv="X-UA-Compatible" content="ie=edge">
   <title>Document</title>
</head>
<script>
   function myprint() {
    const wnd = window.open('about:blank', '', '_blank, alwaysRaised=yes');
    wnd.document.write('<html><head><title></title>');
    wnd.document.write('</head><body>');
    wnd.document.write('<div>JavaScript often abbreviated as JS, is a high-level, interpreted programming language. It is a language which is also characterized as dynamic, weakly typed, prototype-based and multi-paradigm.</div>');
    wnd.document.write('<div class="print-header"><button title="Print" onclick="window.print()">Print</button></div>');
    wnd.document.write('</body></html>');
    wnd.document.close();
  }
</script>
<body>
   <button onclick="myprint()">popup</button>
</body>
</html>

Here Im trying to open my content using window.open() then print them using window.print(). That's all. jsfiddle

This link also not working in ipad chrome. Print

satheesh
  • 403
  • 5
  • 15
  • document.write is NOT like building a string. closing tags are added automatically if you did not include them. So build the string and document.write once – epascarello Aug 29 '18 at 13:11
  • I have tried your answer, it did'n, work. Browser doesn't mind when we use multiple document.write(). – satheesh Aug 30 '18 at 04:32

3 Answers3

1

This is an issue with Chrome on iOS. Because of Apple’s policy on third party browsers, Chrome is actually just a WebView component. Printing is currently not supported. As far as I am aware, there is currently no workaround for this issue.

superluminary
  • 47,086
  • 25
  • 151
  • 148
0

Try this code, if it not works another solution is to use a third-party printing service like this: http://www.printfriendly.com

function goPrint(){
    window.print();
    if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
        window.location.reload();
    }
}
Akeel ahamed
  • 877
  • 11
  • 11
0

You are using a backtick character ` in this line:

wnd.document.write(`<div class="print-header"><button title="Print" onclick="window.print()">Print</button></div>`);

This is a Templated literal and might not be supported. Try replacing it with a single quote:

wnd.document.write('<div class="print-header"><button title="Print" onclick="window.print()">Print</button></div>');
HugoTeixeira
  • 4,674
  • 3
  • 22
  • 32
  • No, it doesn't matter. And also I tried your answer, still same result. – satheesh Aug 29 '18 at 12:35
  • What does it print when you try the solution in this link? https://stackoverflow.com/a/2604997/4289700 – HugoTeixeira Aug 29 '18 at 14:44
  • Thanks for your response. error alert didn't work, alert box didn't even appear. I think browser does't take it as an error. Suggest me if you got any other way. – satheesh Aug 30 '18 at 04:38