最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I print different page using javascriptjQueryajax? - Stack Overflow

programmeradmin1浏览0评论

With following code it's showing the print dialog box and print the page successfully but how do i print a different page after click on this same button ? Different page name is : letterprint.php

<div class="below_movie_left" id="printableArea">
My printing contents
</div>

<input type="button"  class="submit_button" onclick="printDiv('printableArea')" value="Print" style="float:right;" />

<script>
function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}
</script>

Is this possible with javascript / jQuery / Ajax method ? how ?

With following code it's showing the print dialog box and print the page successfully but how do i print a different page after click on this same button ? Different page name is : letterprint.php

<div class="below_movie_left" id="printableArea">
My printing contents
</div>

<input type="button"  class="submit_button" onclick="printDiv('printableArea')" value="Print" style="float:right;" />

<script>
function printDiv(divName) {
     var printContents = document.getElementById(divName).innerHTML;
     var originalContents = document.body.innerHTML;

     document.body.innerHTML = printContents;

     window.print();

     document.body.innerHTML = originalContents;
}
</script>

Is this possible with javascript / jQuery / Ajax method ? how ?

Share Improve this question edited May 26, 2016 at 20:57 marc_s 755k184 gold badges1.4k silver badges1.5k bronze badges asked May 17, 2014 at 3:22 BabuBabu 4552 gold badges14 silver badges33 bronze badges 1
  • Hi!, Possible duplicated here stackoverflow.com/questions/8240472/… – Gonzalo Bahamondez Commented May 17, 2014 at 3:27
Add a comment  | 

2 Answers 2

Reset to default 18

If you already have the page you want to print, put that page in an hidden iframe and print the content of iframe

<iframe src="letterprint.php" name="frame"></iframe>

<input type="button" onclick="frames['frame'].print()" value="printletter">

You can't really print another page; browsers just don't give Javascript that power. However, what you can do is change the content of the page the user is on.

In short, you can use AJAX or an iframe to access the second page (letterprint.php), and then replace the contents of an element on your page with that page's contents. If you don't want it visible to the user, you can use a targeted stylesheet to make the new content only visible when printing.

发布评论

评论列表(0)

  1. 暂无评论