spacer
Yehuda Shiran August 13, 2001
Turning Off Events
Tips: August 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

Defensive programming is a philosophy you should adopt when writing your JavaScript scripts. According to this philosophy, you should never rely on the system to do stuff for you. Instead, you should set your variables yourself, either before or after the relevant event. Let's see an example. Here is the function onPageComplete() that is used to create a dynamic print template:

function onPageComplete() {
  if (event.contentOverflow) {
    document.all("layoutrect" + lastPage).onlayoutcomplete = null;

    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>";
    devicecontainer.insertAdjacentHTML("beforeEnd", newHTML);
    lastPage++;
  }
}
Try it. Works perfectly. Now, let's omit the first line inside the if statement. We'll get the new onPageComplete() function:

function onPageComplete() {
  if (event.contentOverflow) {
    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>";
    devicecontainer.insertAdjacentHTML("beforeEnd", newHTML);
    lastPage++;
  }
}
Try it. You should get one blank page at the end. The reason for this is an extra event of onlayoutcomplete of the previous page that we have already printed. The first line of onPageComplete() sets it to null before printing the next page:

    document.all("layoutrect" + lastPage).onlayoutcomplete = null;
For more information on print templates, go to Column 89, Print Templates, Part I.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business