简体   繁体   中英

How to open an image and have it print?

So I have the code shown below and I want the image to open in a new tab and print right away. But i can't seem to get that too work. Any help is appreciated.

function Print(){
     window.open('images/couponPrintSmall.jpg');
     window.onLoad = window.print();
}

Try:

function Print(){
     var win = window.open('images/couponPrintSmall.jpg' , "win");
     win.onload = win.print;
}

Actually , this one works:

function Print(){
    var win = window.open('' );
    var img=win.document.createElement("img");
    img.src="http://www.w3schools.com/images/w3schoolslogoNEW310113.gif";
    win.document.body.appendChild(img);
    img.onload = function(){ 
        win.print();
    };  
}

Fiddle: http://jsfiddle.net/qPYw6/7/

I've found that the best fix for this problem is simply to link the text to a separate page. And then in the body put a onLoad = window.print(). It was much easier, and I figured I would answer my own question seeing it still had no correct answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM