【问题标题】:Window.print() not working in ipad chrome in popup window[window.open()]Window.print() 在弹出窗口中的 ipad chrome 中不起作用[window.open()]
【发布时间】:2018-08-29 12:18:01
【问题描述】:

下面是我的一段代码,它可以在所有操作系统的所有浏览器中运行,除了 ipad chrome。帮帮我吧。

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

我在这里尝试使用window.open() 打开我的内容,然后使用window.print() 打印它们。就这样。 jsfiddle

此链接在 ipad chrome 中也不起作用。 Print

【问题讨论】:

  • document.write 不像构建字符串。如果您没有包含结束标签,则会自动添加它们。所以构建字符串和document.write一次
  • 我已经尝试过你的答案,它没有,工作。当我们使用多个 document.write() 时,浏览器并不介意。

标签: javascript html


【解决方案1】:

试试这个代码,如果它不起作用,另一个解决方案是使用这样的第三方打印服务:http://www.printfriendly.com

function goPrint(){
    window.print();
    if(navigator.userAgent.toLowerCase().indexOf('chrome') > -1) {
        window.location.reload();
    }
}

【讨论】:

    【解决方案2】:

    您在此行中使用了反引号 `

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

    这是Templated literal,可能不受支持。尝试用单引号替换它:

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

    【讨论】:

    • 不,没关系。我也试过你的答案,结果还是一样。
    • 当您尝试此链接中的解决方案时会打印什么? stackoverflow.com/a/2604997/4289700
    • 感谢您的回复。错误警报不起作用,甚至没有出现警报框。我认为浏览器不会将其视为错误。如果你有任何其他方法,建议我。
    【解决方案3】:

    这是 iOS 上的 Chrome 的问题。由于 Apple 对第三方浏览器的政策,Chrome 实际上只是一个 WebView 组件。当前不支持打印。据我所知,目前没有解决此问题的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 2016-07-18
      • 2011-03-13
      • 1970-01-01
      相关资源
      最近更新 更多