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


628 VISUALBASICSCRIPTINGEDITION(VBSCRIPT)

shell services including the registry, log files, environ-
ment variable values, and system folders (the latter two
functions are implemented through the use of child ob-
jects, WshEnvironment, and WshSpecialFolders). The
WshShell object also allows your script to create and
save shortcuts, invoke other applications, and simu-
late the sending of keystrokes to another application.
Listing 4 provides the code for a script that uses the
WScript.ScriptFullName and WScript.ScriptName prop-
erties and the intrinsic VBScript “Replace()” function to
construct a reference to a file named “temp.txt” located
in the same directory as the script. (This assumes that
the value of WScript.ScriptName appears exactly once
in value of WScript.ScriptFullName.) Using the Script-
ing Runtime Library’s FileSystemObject this script then
displays the first line of text contained in temp.txt, or if
the file does not exist, it creates the file, writes text to it,
and then saves it.
The spate of computer viruses that exploited VBScript
and the WSH, led Microsoft to introduce a new, beefed-
up security model as part of WSH 5.6 (Clinick, 2000).
This new model allows users (or system administrators)
to set a trust policy for scripts submitted to WSH. Us-
ing a particular registry key (\HKEYCURRENTUSER\
SOFTWARE\Microsoft\Windows Script Host\Settings\
TrustPolicy), users can enable running of any script
in WSH, allow only scripts signed by a trusted third
party (i.e., someone in your Trusted Publishers List) to
run under WSH on your machine, or prompt before a
script not signed by a trusted third party is run. To sign
scripts you must have a valid certificate (typically from
a commercial certification authority). Script signing is
accomplished programmatically using the Signer object,
which is part of the latest Microsoft Scripting Runtime
Library.

Internet Explorer Scripting Host
Object Model
The VBScript scripting host object model for Internet
Explorer differs significantly from those for IIS, WSH,
and Outlook. As just mentioned it is only implemented
on Internet Explorer, and it is not presently available on
Netscape Navigator. Client-side script is embedded within
HTML (specifically inside the HTML <Script> element),
and it is intended to interact closely with HTML objects
(elements). The Internet Explorer scripting host object
model is derived largely from the W3C DOM for HTML,
but it adds more support for events and a few other fea-
tures to facilitate use of the DOM with scripting. Object
events play a particularly important role in the Internet
Explorer object model. The top-level object in the Inter-
net Explorer scripting host object model is the “Window”
object (referring to the Web browser window, not the oper-
ating system). The most important child of Window is the
“Document” object, which contains collections and child
objects relating to all the HTML elements on the Web page
displayed by the browser, including especially all objects
in any HTML form or forms on the page (i.e., those with
which the user can interact). The Window object also in-
cludes properties and child objects that provide context
for the document (e.g., current URL, previously viewed

pages, information about frames in use, parent windows,
etc.).
Client-side VBScript can be used for a variety of pur-
poses, but most commonly it is used for validation of data
entry on HTML forms, manipulation of HTML content on
a page (e.g., hiding and showing certain options, chang-
ing text and values of elements and/or form menus in re-
sponse to user actions), and extended interactions with
Web clients beyond those inherently supported by the
Web browser itself (including those invoked from exter-
nal COM objects). For example, VBScript can be used to
validate input data at time of form submittal, to validate
each entry and menu selection as it is being made, to up-
date menus and form structure as specific selections are
made (e.g., to give only compatible selections), or to fa-
cilitate browsing of a resource via a table of contents. An
illustration of the last scenario would be an application
that provided two frames, one smaller, narrower frame
showing the table of contents for the work, and a sec-
ond larger frame for showing the text of a given subsec-
tion of the work. Script could then be written to allow the
user to expand and hide sections of the table of contents
and to synchronize the text shown in the larger frame
as items in the table of contents frame are selected and
deselected.
The complete Internet Explorer object model is too ex-
tensive to document in its entirety here, but a few specifics
are worth highlighting. Script not contained in a subrou-
tine (e.g., not intended to handle a specific event) is exe-
cuted as encountered and can be used to help construct
or modify the HTML page as it is being rendered on the
client side. Other script is invoked only in response to cer-
tain events, e.g., page loading or unloading, form submit-
tal, or events associated with form elements such as when
a text box gets focus, when a check box is checked, when
a selection from a menu is made or changed. The same
HTML element may be examined and/or manipulated us-
ing different components of the Internet Explorer ob-
ject model. (Thus a form element may be accessed either
through the Window.Document.All collection or through
the Window.Document.Forms collection.) Available prop-
erties, methods, and events may be different for different
HTML elements, though nearly all HTML elements share
properties such as InnerHTML (content of the element
including HTML markup), InnerText (content of the el-
ement stripped of HTML markup), ParentElement (im-
mediate HTML parent to the element), NextSibling (next
HTML element having same parent), and PreviousSib-
ling (previous HTML element having same parent), and
collections such as Attributes (collection of all attributes
of the element), ChildNodes (collection of all HTML ele-
ments that are immediate children of the element), and
All (collection of all HTML elements contained in the ele-
ment). Generally, to be most easily manipulated in script,
HTML elements should be assigned unique id attributes
(all elements on an HTML page) and/or unique name at-
tributes (form and anchor elements). HTML form ele-
ments typically will have values and text content associ-
ated which may change in response to user actions. For
instance, checkable form elements (e.g., radio buttons and
check boxes) will have a Boolean checked property to in-
dicate whether the button or box has been checked, while
Free download pdf