简体   繁体   中英

Open images in a new page and print when clicked

Here is my page: http://budclarychevy.com/custom/parts-specials-test

My aim is to have the images clickable and open in a new window with a prompt to print. The closest method I can find is the window.print() function in JavaScript but that just prints the entire page. I'd like each image to open on a printable page by itself. Is this possible with JavaScript? If there's another method, what is it?

you can simply add an onclick event with some javascript.

<img src="http://budclarychevy.com/path/to/my/image.png"
width="660" height="225" alt="GM Oil Filters"
onclick="newWindow = window.open('http://budclarychevy.com/path/to/img.png');
    newWindow.print();">

As you can see, on a click a new window is opened with the url of the picture. As you have a reference to the new window object, you can call the 'print()' function on it.

Cheers Laidback

You could do something like

function clicked(address) {

    popup = window.open(); // display popup
    popup.document.write(address.src); // This is where the image url goes which will just open up the image
    popup.print(); // then print the image

}

in HTML

<img src="image.jpg" onclick="clicked(this)" />

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