Print Templates, Part IV: User's Settings: Template6's Code Listing - Doc JavaScript | WebReference

Print Templates, Part IV: User's Settings: Template6's Code Listing - Doc JavaScript


Print Templates, Part IV: User's Settings

Template6's Code Listing

<HTML XMLNS:IE>
<HEAD>
<?IMPORT NAMESPACE="IE" IMPLEMENTATION="#default">
<STYLE TYPE="text/css">
.contentstyle
{
  width:5.5in;
  height:8in;
  margin:1in;
  background:white;
  border:1 dashed gray;
}
.masterstyle
{ 
  background:#FFFF99;
  border-left:1 solid black;
  border-top:1 solid black;
  border-right:4 solid black;
  border-bottom:4 solid black;
  width:8.5in;
  height:11in;
  margin:10px;
  overflow:hidden;
}
.headerstyle
{
    position:absolute;
    top:.25in;
    width:6in;
    left:1in;
}
.footerstyle
{
  position:absolute;
  top:11.0in;
  width:6in;
  left:1in;
}
</STYLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
var lastPage = 1;
var oMasterStyleClass;
var oContentStyleClass;
var oHeaderStyleClass;
var oFooterStyleClass;
function findStyleRule(styleName) {
  for (i = 0; i < document.styleSheets.length; i++) { 
    for (j = 0; j < document.styleSheets(i).rules.length; j++) {
      if (document.styleSheets(i).rules(j).selectorText == styleName) {
        return document.styleSheets(i).rules(j);
	  }
    }     
  }
}
function init() {
  addFirstPage();
  oMasterStyleClass = findStyleRule(".masterstyle");
  oContentStyleClass = findStyleRule(".contentstyle");
  oHeaderStyleClass = findStyleRule(".headerstyle");
  oFooterStyleClass = findStyleRule(".footerstyle");
            
  oMasterStyleClass.style.width = printer.pageWidth/100 + "in";
  oMasterStyleClass.style.height = printer.pageHeight/100 + "in";  
  oContentStyleClass.style.marginTop = printer.marginTop/100 + "in"; 
  oContentStyleClass.style.marginLeft = printer.marginLeft/100 + "in";
  oContentStyleClass.style.width = (printer.pageWidth - (printer.marginLeft + printer.marginRight))/100 + "in";
  oContentStyleClass.style.height = (printer.pageHeight - (printer.marginTop + printer.marginBottom))/100 + "in";
  oHeaderStyleClass.style.left = printer.marginLeft/100 + "in";
  oHeaderStyleClass.style.top = printer.unprintableTop/100 + "in";
  oHeaderStyleClass.style.width = oContentStyleClass.style.width;   
  oFooterStyleClass.style.left = printer.marginLeft/100 + "in";
  oFooterStyleClass.style.width = oContentStyleClass.style.width;
  oFooterStyleClass.style.bottom = printer.unprintableBottom/100 + "in";
}
invocations = 0;
function CheckIfPrintRequested() {
  invocations++;
  if (invocations > 1) return;
  switch (dialogArguments.__IE_PrintType) {
    case "Prompt":
      if (printer.showPrintDialog()) printPrep();
      break;
    case "NoPrompt":
      printPrep();
      break;
    case "Preview":
    default:
	break;
  }
}
function addFirstPage() {
  newHTML  = "<IE:DEVICERECT ID='devicerect1' MEDIA='print' CLASS='masterstyle'>";
  newHTML += "<IE:LAYOUTRECT ID='layoutrect1' CONTENTSRC='document' ONLAYOUTCOMPLETE='onPageComplete()' NEXTRECT='layoutrect2' CLASS='contentstyle'/>";
  newHTML += "</IE:DEVICERECT>";
  pagecontainer.insertAdjacentHTML("afterBegin", newHTML);
  myheaderfooter.textHead = printer.header;
  myheaderfooter.textFoot = printer.footer;
  myheaderfooter.url = dialogArguments.__IE_BrowseDocument.URL;
  myheaderfooter.title = dialogArguments.__IE_BrowseDocument.title;
  myheaderfooter.page = 1;
  addHeaderAndFooterToPage(1);
}
function onPageComplete() {
  if (event.contentOverflow == true) {
    addNewPage();
  }
  else {
    myheaderfooter.pageTotal = document.all.tags("DEVICERECT").length;
        
    for (i = 1; i <= myheaderfooter.pageTotal; i++) {
      addPageTotalToPages(i);
	}
            
    setTimeout("CheckIfPrintRequested();", 100);
  }
}
function addNewPage() {
  document.all("layoutrect" + lastPage).onlayoutcomplete = null;
  myheaderfooter.page = lastPage + 1;
    
  newHTML  = "<IE:DEVICERECT ID='devicerect" + (lastPage + 1) +  "' MEDIA='print' CLASS='masterstyle'>";
  newHTML += "<IE:LAYOUTRECT ID='layoutrect" + (lastPage + 1) + "' ONLAYOUTCOMPLETE='onPageComplete()' NEXTRECT='layoutrect" + (lastPage + 2) + "'  CLASS='contentstyle'/>";
  newHTML += "</IE:DEVICERECT>";
  pagecontainer.insertAdjacentHTML("beforeEnd", newHTML);
  addHeaderAndFooterToPage(lastPage + 1);
  lastPage++;
}
function addHeaderAndFooterToPage(pageNum) {
  newHeader = "<DIV CLASS='headerstyle'>" + myheaderfooter.htmlHead + "</DIV>";
  newFooter = "<DIV CLASS='footerstyle'>" +myheaderfooter.htmlFoot + "</DIV>";
    
  document.all("devicerect" + pageNum).insertAdjacentHTML("afterBegin", newHeader); 
  document.all("devicerect" + pageNum).insertAdjacentHTML("beforeEnd", newFooter);
}
function addPageTotalToPages() {
  oSpanCollection = document.all.tags("span");
  for (i = 0; i < oSpanCollection.length; i++) {
    if (oSpanCollection[i].className == "hfPageTotal") {
      oSpanCollection[i].innerText = myheaderfooter.pageTotal;
    }
  }
}
function printPrep() {
  if (layoutrect1.contentDocument.readyState == "complete") {
	printNow();
  }
  else {
    layoutrect1.contentDocument.onreadystatechange = printWhenContentDocComplete;
	}
}
function printWhenContentDocComplete() {
  if (layoutrect1.contentDocument.readyState == "complete")	{
    layoutrect1.contentDocument.onreadystatechange = null;
    printNow();
  }
}
function printNow() {
  var startPage;
  var endPage;
  var oDeviceRectCollection = document.all.tags("DEVICERECT");
    
  if (dialogArguments.__IE_PrintType == "NoPrompt" || 
        printer.selectedPages == false) {
    startPage = 1;
    endPage = oDeviceRectCollection.length;
  }
  else if (printer.currentPage == true) {
    }
    else {
      startPage = printer.pageFrom;
      endPage = printer.pageTo;
      if (startPage > endPage) {
        alert("Error: Start page greater than end page");
        return;
      }
      if (startPage > oDeviceRectCollection.length) {
        alert("Error: Start page greater than number of pages in print job.");
        return;
      }
      if (endPage > oDeviceRectCollection.length) {
        alert("Warning: End page greater than number of pages in print job. Continuing Print Job.");
        endPage = oDeviceRectCollection.length;
      }
    }
    
	printer.startDoc("Printing from template6.htm");
    for (i = startPage - 1; i < endPage; i++) {
	  printer.printPage(oDeviceRectCollection[i]);
	}
        
	printer.stopDoc();
}
// -->
</SCRIPT>
<HEAD>
<BODY ONLOAD="init()">
<IE:TEMPLATEPRINTER ID="printer"/>
<IE:HEADERFOOTER ID="myheaderfooter"/>
<DIV ID="pagecontainer">
<!-- Dynamically created pages go here. -->
</DIV>
</BODY>
</HTML>

Next: A Final Word

http://www.internet.com


Produced by Yehuda Shiran and Tomer Shiran
All Rights Reserved. Legal Notices.
Created: September 24, 2001
Revised: September 24, 2001

URL: http://www.webreference.com/js/column93/7.html