Using javascript to print images

前端 未结 9 913
清歌不尽
清歌不尽 2020-12-02 17:40

I would like to know if it\'s possible to use javascript to open a popup window containing an image, and at the same time have the print dialog show. Once someone clicks on

9条回答
  •  Happy的楠姐
    2020-12-02 18:06

    I just spent 45 minutes on this "SIMPLE" problem, trying to get it the way I wanted it to operate.

    I had an image inside an img tag, dynamically generated by a jQuery Barcode plugin that I had to print. I wanted it to print in another window and afterwards close the window. This was all supposed to happen after the user clicked a button inside a jQuery Grid plugin, inside a jQuery-UI dialog along with jQuery-UI dialog extender applied to it.

    I adjusted everyone answers till I finally came up with this, maybe it can help someone.

    w = window.open(document.getElementById("UB-canvas").src);
    w.onload = function () { w.print(); }
    w.onbeforeunload = setTimeout(function () { w.close(); },500);
    w.onafterprint = setTimeout(function () { w.close(); },500);
    

    The setTimeout is not just for shits and giggles, it's the only way I found Firefox 42 would hit those functions. It would just simply skip the .close() functions until I added a breakpoint to it, then it worked perfectly. So I'm assuming it created those window instances before it could apply the onbeforeload event function and onafterprint event functions, or something.

提交回复
热议问题