Using window.print() or alternative on Android devices

后端 未结 7 450
一生所求
一生所求 2020-12-09 18:55

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

7条回答
  •  被撕碎了的回忆
    2020-12-09 19:34

    I'm working on a simular problem and came up with this solution:

    $(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;
      });
    });
    
    

    I haven't had the time to check if this works, i don't have an android device with me at the moment. I Would love to have some feedback on this ;-)

提交回复
热议问题