Using javascript to print images

前端 未结 9 900
清歌不尽
清歌不尽 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条回答
  •  [愿得一人]
    2020-12-02 18:25

    This code will open YOUR_IMAGE_URL in a popup window, show print dialog and close popup window after print.

    var popup;
    
    function closePrint () {
        if ( popup ) {
            popup.close();
        }
    }
    
    popup = window.open( YOUR_IMAGE_URL );
    popup.onbeforeunload = closePrint;
    popup.onafterprint = closePrint;
    popup.focus(); // Required for IE
    popup.print();
    

    MDN Reference code

提交回复
热议问题