The Internet Encyclopedia (Volume 3)

(coco) #1

P1: JDT


WL040C-44 WL040/Bidgoli-Vol III-Ch-51 June 23, 2003 16:33 Char Count= 0


OBJECTS INVBSCRIPT 629

select menus will have a selected index property to indi-
cate which menu item(s) has been selected. Script can be
used to insert HTML elements into the DOM for a page
at the time the page is loaded or later, in response to a
particular user action.
Selection of which components of the Internet Ex-
plorer object model to use depends largely on the purpose
of the script, the exact nature of the HTML elements to
be examined or manipulated, and the way the HTML is
written. For instance, the Window.Document.All collec-
tion provides access to every HTML element on the Web
page. Using the All collection, HTML elements may be ac-
cessed by value of id attribute (if present), by the element’s
position on the page (i.e., the sequence of the elements on
the page relative to all HTML elements on the page), by
value of the name attribute (but only if the element is con-
tained in a form), or by element tag name. When retriev-
ing by name attribute, tag name, or id attribute (since for
HTML Internet Explorer does not enforce the uniqueness
of id attributes), a subcollection may be retrieved rather
than a single HTML element reference if more than one
element has the tag name or attribute specified. To get the
specific HTML element of interest requires that the pro-
grammer either also specify its sequence among all those
with the same name attribute, tag name, or id attribute or
iterate through the subcollection until the HTML element
with the desired property is found.
Among alternatives to the Window.Document.All col-
lection are: the anchors collection, which gives ac-
cess to all HTML <a> elements having nonnull name
attribute value and/or nonnull id attribute value; the
links collection, all HTML <a> elements having a non-
null href attribute, and all HTML <area> elements; the
forms collection, all HTML <form> elements (including

Figure 2: What the user sees when running the script in List-
ing 5.

children); the images collection, all HTML <img> ele-
ments; and several other specialized subcollections of
HTML page elements. Note also that the Internet Ex-
plorer Object model supports shorthand syntax. For in-
stance, forms can be referred to by name directly or
as an item in the Window.Document.Forms collection.
Thus, the HTML form element “<Form name = ‘my-
Form’>” may be referenced in script using either “Win-
dow.Document.myForm” or Window.Document.Forms.
Item(“myForm”).” Other forms of shorthand are sup-
ported as well, though overuse of such shorthand can
reduce code clarity. Listing 5 illustrates a client-side VB-
Script that uses script to populate a select menu on a form
when the page is first loaded. VBScript is also used to in-
sert an additional label and text box on the form when
“other” is selected from the pull-down menu by the user.
Figure 2 shows what the user sees when running the script
in Listing 5.

Listing 5:Illustrating client-side VBScript in Internet Explorer.
<html>
<head><script Language='VBScript'>
sub window_onLoad
set objMyTag = Window.Document.CreateElement("option")
Window.Document.myForm.Item("myMenu").Options.Add (objMyTag)
objMyTag.innerText="blue"
set objMyTag = Window.Document.CreateElement("option")
Window.Document.myForm.Item("myMenu").Options.Add (objMyTag)
objMyTag.innerText="green"
set objMyTag = Window.Document.CreateElement("option")
Window.Document.myForm.Item("myMenu").Options.Add (objMyTag)
objMyTag.innerText="other"
end sub
sub myMenu_onChange
if Window.Document.myForm.Item("myMenu").SelectedIndex = 2 then
set objMyTag = Window.Document.CreateElement("span")
document.forms.item("myForm").appendChild (objMyTag)
objMyTag.innerText = "Specify Color:"
set objMyAtt = Window.Document.CreateAttribute("id")
objMyAtt.value = "OtherColorLabel"
objMyTag.Attributes.setNamedItem (objMyAtt)
set objMyTag = Window.Document.CreateElement("Input")
document.forms.item("myForm").appendChild (objMyTag)
set objMyAtt = Window.Document.CreateAttribute("type")
Free download pdf