• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1714
  • Last Modified:

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.
1 Solution
The best way I can think of is to popup windows. The windows themselves, in their <body> should have onload="opener.focus();" to lose focus. (or self.blur(); if you want)

The first page you want to print would have this:
<body onload="opener.focus();window.print();self.close();" onunload="window.open('nextPrintPage.html','printPage','');">

Each following printed page would have the same thing. The last printed page will just not have the onunload at all.

Of course, each of this displays the print dialog. If you want it gone (and I'm warning you that if you don't tell the users clearly there will be no dialog, you'll get tons of negative feedback):


my accepted answer
You can load the pages into hidden layers and then using styling to make them visible to the printer as needed:

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

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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now