什么是获得一个图像点击时在新窗口中打印的最简单的方法? [英] What's the simplest way to get an image to print in a new window when clicked?

查看:175
本文介绍了什么是获得一个图像点击时在新窗口中打印的最简单的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个简单的点击打印链接的图像,然后我想什么发生是在点击链接时,一个新的窗口与图像打开,打开浏览器的打印命令对话框。

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.

我的问题是,这是否是可能刚刚从URL参数,或者从锚元素中发起页面上?还是我必须建立与JavaScript的一个目标页面做到这一点?

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>

显然,code以上将在新窗口中打开图像,但仍要求用户按Ctrl + P键,Cmd + 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?

推荐答案

您必须调用window.print()。也许这样的事情?

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>

这篇关于什么是获得一个图像点击时在新窗口中打印的最简单的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆