在安卓设备上(我测试过Nexus5、Nexus10、Galaxy S4和Galaxy Tab3),JavaScript中的window.print()命令什么也做不了。据我所知,它甚至没有记录错误。
我知道的事实是,这些浏览器中的大多数都可以打印,因为你可以使用手机Chrome的菜单选择“打印”。
为什么window.print()不能触发您期望的行为(打开客户端打印菜单)?有没有可以替代window.print()的安卓系统
发布于 2014-11-06 11:40:42
这个Documentation中清楚地写着:“这个命令在iOS上支持,在Windows和Safari上支持,在Mac上支持,在Android上不支持。。”
安卓手机还没有原生的打印支持,所以window.print()将无法工作。这意味着您需要使用第三方应用程序进行打印。您可以在此article中找到一些替代方案。
发布于 2015-03-06 17:27:46
我正在研究一个模拟问题,并提出了这个解决方案:
$(document).ready(function($) {
var ua = navigator.userAgent.toLowerCase();
var isAndroid = ua.indexOf("android") > -1; //&& ua.indexOf("mobile");
$('button.print').click(function(e) {
e.preventDefault();
if (isAndroid) {
// https://developers.google.com/cloud-print/docs/gadget
var gadget = new cloudprint.Gadget();
gadget.setPrintDocument("url", $('title').html(), window.location.href, "utf-8");
gadget.openPrintDialog();
} else {
window.print();
}
return false;
});
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<button class="print">Print this page</button>
我还没来得及检查这是否能用,我现在还没有安卓设备。我希望得到一些反馈;-)
发布于 2014-11-10 13:13:23
⚠️已弃用:自2020年12月31日起,将不再支持Google Cloud Print。有关迁移的帮助,请参阅support article。
使用Google Cloud Print (GCP) -不需要应用程序。不过,用户必须已经通过GCP设置了打印机。
此示例使用GCP gadget
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Print</title>
</head>
<body>
<div>
<p>On android devices (I have tested Nexus 5, Nexus 10, Galaxy S4 and Galaxy Tab 3) the window.print() command in javascript doesn't do anything, as far as I can tell it doesn't even register an error.</p>
<p>I know for a fact that most if not all of these browsers can print because you can use mobile chromes menu to choose "print". My questions is, why doesn't window.print() trigger the behavior you would expect (opening the clients print menu).
And is there an android alternative to window.print()?</p>
</div>
<div id="gcpPrint"></div>
<script src="https://www.google.com/cloudprint/client/cpgadget.js">
</script>
<script>
var gadget = new cloudprint.Gadget();
gadget.setPrintButton(cloudprint.Gadget.createDefaultPrintButton("gcpPrint"));
gadget.setPrintDocument("text/html", "Print", document.documentElement.innerHTML);
</script>
</body>
</html>https://stackoverflow.com/questions/26684190
复制相似问题