Automatically Print Image from Website

前端 未结 9 731
一生所求
一生所求 2020-12-05 05:37

A coworker and I were having a discussion about what is and isn\'t possible within the browser.

Then a question came up that neither of us could answer with certaint

9条回答
  •  借酒劲吻你
    2020-12-05 06:05

    You have to prompt the user to print the current page, there's no way to bypass this step (possibly in activeX for IE). That said, there's two different ways you could prompt the user to print images of you smiling when the page is loaded.

    Here's how to do it in JavaScript.

    window.onload = function() {
      var img = window.open("me-smiling.png");
      img.print();
    }
    

    And here's how to do it in css/javascript/html (assuming your picture has the id 'me-smiling'): CSS:

    @media print {
       * {
         display:none;
       }
       img#me-smiling {
         display:block;
       }
    }
    

    Javascript:

     window.onload = function() { window.print() }
    

提交回复
热议问题