Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the simplest way to get an image to print in a new window when clicked?

I'm trying to create a simple click to print link for an image, and what I'd like to happen is when the link is clicked, a new window opens with the image, and the browser opens the print command dialogue box.

My question is whether this is possible just from a URL parameter, or from the anchor element on the initiating page? Or do I have to build a target page with javascript to do this?

Here's a sample of what I've got:

<p class="click-2-print">
  <a href="/img/map.jpg" target="_blank">Click here to print the map above</a>
</p>

Obviously the code above will open the image in a new window, but still requires to user to hit Ctrl+P, Cmd+P or use the browser commands. My client wants the image to "just print" when the user clicks the link, so I'm trying to find the simplest way to accomplish this.

So is there any parameters or attributes that I can add to the above markup to accomplish what I have described?

like image 422
Joel Glovier Avatar asked Feb 22 '11 02:02

Joel Glovier


People also ask

How do I make an image appear in a new tab?

Note that you can also middle-click "View Image" apart from Ctrl + left-click to open the image in as new tab. Note that you can also middle-click "View Image" apart from Ctrl + left-click to open the image in as new tab.

How do I print directly from the browser without popping up a pop up window?

You just need to call Print() itself, instead of window. print() in the onclick event.


1 Answers

You'll have to call window.print(). Perhaps something like this?

function printimage(){
    var URL = "http://myimage.jpg";

    var W = window.open(URL);

    W.window.print();
}

Another option may be to put the image on a page that tells the browser to print when loaded. For example...

<body onload="window.print();">
<img src="/img/map.jpg">
</body>
like image 161
Cody Bonney Avatar answered Oct 11 '22 05:10

Cody Bonney