Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print different page using javascript/jQuery/ajax?

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 ?

like image 742
Babu Avatar asked May 17 '14 03:05

Babu


People also ask

How do I print a Web page using jQuery?

jQuery Code:$('#sudo). click(function(){ window. print(); return false; });

How do I print a specific part of a webpage using JavaScript?

printPageArea() function contains some JavaScript code which helps you to implement print feature easily in the web page. It will provide the simplest way to print specific area of web page.

How do you print screen in JavaScript?

Window print() Method The print() method prints the contents of the current window. The print() method opens the Print Dialog Box, which lets the user to select preferred printing options.


2 Answers

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">
like image 51
coder hacker Avatar answered Nov 04 '22 10:11

coder hacker


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.

like image 37
machineghost Avatar answered Nov 04 '22 11:11

machineghost