August 13, 2001 - Turning Off Events | WebReference

August 13, 2001 - Turning Off Events

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

Yehuda Shiran, Ph.D.
Doc JavaScript

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.