The Body of a Scriptlet

Since a Scriptlet is ultimately an HTML page, we can view it through any browser – though what we actually see won't be interesting in most cases. In other words, not all the Scriptlets are self-initialized and display something useful without being scripted. If we have a component that displays a user-specified text string in a particular fashion, we need to provide that text string before using the Scriptlet. Thus, outside its host site, it will probably show a default string which might be empty or contain copyright information.

The body can include images, links, lines, text, and even ActiveX controls, applets, sounds, and anything else. It represents the user interface of the module, and includes the constituent elements on which the script code within the Scriptlet will act. The actual body may be defined at design-time, or created dynamically depending on what we really need to do.

For example, if we want a dynamic text string to become a Scriptlet, we might want to have a body like this:

<body>
  <span id="text"> Change this text at runtime through a property. </span>
</body>

We change the actual text using an exposed attribute that ends up setting the outerText property of the given element. The following VBScript code that does just that:

Sub DoSetText( sText ) 
  Set coll = document.all.tags("SPAN")
  coll.item(0).outerText = sText 
End Sub

Alternately, we could also have an empty body which will be filled when needed with an HTML string:

<body>
</body>
Sub DoSetText(sText) 
  sPrefix = "<span id=text>"
  sSuffix = </span> 
  sHtmlText = sPrefix + sText + sSuffix 
  Set coll = document.all.tags("SPAN")
  if coll.length > 0 then
    coll.item(0).outerHtml = sHtmlText 
  else
    document.body.insertAdjacentHTML "AfterBegin", sHtmlText
  end if
End Sub

In the above sample, we insert a new SPAN element if one does not exist, or change the existing one if it is already there.

© 1997 by Wrox Press. All rights reserved.