简体   繁体   English

使用jQuery打开图像以在新窗口中打印

[英]Open image to print in new window using jQuery

I have been trying to get this seemingly simple jQuery to work. 我一直试图让这个看似简单的jQuery工作。 The plan is to have an external image, which is a smaller version of a large promotional landing page, open in a new window with a print dialog and then close after printing (or canceling). 计划是拥有一个外部图像,这是一个大型促销登陆页面的较小版本,在带有打印对话框的新窗口中打开,然后在打印(或取消)后关闭。 This is the closest I can get it to work ( jsfiddle ): 这是我最接近它的工作( jsfiddle ):

$('#printBtn').click(function() {
    var couponWindow=window.open('','couponWindow','width=580,height=710');
    couponWindow.focus();
    couponWindow.print();
    couponWindow.close();
});

I have tried numerous attempts and variations to open the image into the new window including: 我已经尝试了许多尝试和变体来将图像打开到新窗口,包括:

var couponWindow=window.open(this.href('http://ecommerce.maurices.com/str/pzTesting/mysteryOffer/coupon.gif'),'couponWindow','width=580,height=710');

and

var URL = $.myURL("index", $(this).attr('http://ecommerce.maurices.com/str/pzTesting/mysteryOffer/coupon.gif'));
    window.open(URL,'couponWindow','width=580,height=710');

All of my attempts to add the image to the window either produces no window, the window opens and closes immediately, or the window opens with the image but doesn't show the print dialog. 我将图像添加到窗口的所有尝试都不产生窗口,窗口立即打开和关闭,或者窗口打开图像但不显示打印对话框。 I have the "coupon" image hosted externally and have also tried with a basic HTML file with the image wrapped in a div. 我有外部托管的“优惠券” 图像 ,并尝试使用基本的HTML文件 ,图像包含在div中。 What am I missing? 我错过了什么?

There is no need to open up a new window. 没有必要打开一个新窗口。 CSS Print Media is all you need. CSS Print Media就是您所需要的。

 $("button").on("click", function() { $("#coupon").off("load").on("load", function() { window.print(); }).attr("src", "http://www.cdc.gov/animalimportation/images/dog2.jpg"); }); 
 body #coupon { display: none; } @media print { body * { display: none; } body #coupon { display: block; } } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <p>This will not print</p> <img id="coupon" /> <button>Print coupon</button> 

Create a new HTML file "print-coupon.html" that explicitly invokes the print command and have that HTML file open in a new window. 创建一个新的HTML文件“print-coupon.html”,它显式调用print命令并在新窗口中打开该HTML文件。

<img src="http://ecommerce.maurices.com/str/pzTesting/mysteryOffer/coupon.gif" />
<script>
    window.print();
</script>

http://jsfiddle.net/hpswddey/ http://jsfiddle.net/hpswddey/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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