Link to home
Start Free TrialLog in
Avatar of niner
niner

asked on

Printing multpile pages with javascript

Is there an easy way of printing a range of html pages from a javascript on the first page ?
Ideally this should not involve loading those pages visibly into the browser, but this might be too much to ask.
ASKER CERTIFIED SOLUTION
Avatar of Zyloch
Zyloch
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can load the pages into hidden layers and then using styling to make them visible to the printer as needed:

<html>
<head>
<title> multi page print </title>
<style type="text/css">
@media print {
.printyes {display:block}
.printno {display:none}
}
</style>
<script type="text/javascript">
var pagecnt=6;
var str='page0';
var i=0;
function printMulti()
{
   window.print();
   document.getElementById(str).className='printno';
   i++;
   str='page'+i;
   document.getElementById(str).className='printyes';
   if (i<pagecnt)
   setTimeout('printMulti()',2000);
}
</script>
</head>
<body>
<div id="page0" class="printyes">
Main page of content here and visible on the screen
</div>
<div id="page1" class="printno" style="display="none">
page 1 content not displayed on the screen
</div>
<div id="page2" class="printno" style="display="none">
page 2 content not displayed on the screen
</div>
<div id="page3" class="printno" style="display="none">
page 3 content not displayed on the screen
</div>
<div id="page4" class="printno" style="display="none">
page 4 content not displayed on the screen
</div>
<div id="page5" class="printno" style="display="none">
page 4 content not displayed on the screen
</div>
</body>
</html>


The timeout between pages is to give enough time for the page to transfer before chaning the setup for the next page. The only page that needs any cod on it is the first page.  If the pages are actually seperate files you will probably need to use XMLHTTP to load the layers in background, or perhaps a hidden frame/iframe.

I would not advise doing this unless the users are aware that they are going to get additional pages when they print; as you could make a lot of people angry and get your site on lists of bad sites that should not be re-visited.

Cd&