1. In the HTML page you've provided contains two body elements though the browser will never show any error, you need to avoid such issues.
2. You will not be able to print the content of two different pages using "window.print()" in another page. "window.print()" is supposed to print the content of the page in which you've furnished that code. For printing some other pages from another page you have to use some sort of frames or AJAX methods.
Below you can find a iframe based method using which you can print two different files from a third file:
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Untitled Document</title>
<style type="text/css">
</style>
<script type="text/javascript">
function printPg(ifrmname){
if(top.frames[ifrmname]){
top.frames[ifrmname].focus();
top.frames[ifrmname].print();
}
}
</script>
</head>
<body>
<h1>This is the main file from which I want to print two other files</h1>
<div>
<iframe name="one" width="0" height="0" frameborder="0" src="1.htm"></iframe>
<iframe name="two" width="0" height="0" frameborder="0" src="2.htm"></iframe>
</div>
<div>
<a href="#" onclick="printPg('one');return false;">Print One</a>
<a href="#" onclick="printPg('two');return false;">Print Two</a>
</div>
</body>
</html>
(a) In the first iframe I've mentioned the first page which needs to be printed. Like that in the second iframe I've mentioned the second page that needs to be printed.
(b) The width and height of both iframes kept as "0" as we don't want to display the first and second page content in my third file( the file from which am going to print).
(c) From the anchor element's onclick event I've called a JS function and passed the name of the iframes into the function.
Now you should be able to view the printer dialog box. I've checked it in IE 7 and FF 3.0.1 in both it works correctly.
Please find all the files I've used in this case as attachment. Unzip them, keep them together and browse "main.htm" and try to click the links available in the page.
Hope this helps.
Bookmarks