Understanding DHTML, Cascading Style Sheets, and Scripting
This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
Even if you are an experienced Office and VBA developer, you might approach this subject with a little apprehension. If you have no experience with using DHTML or adding script to Web pages, this section heading alone may be somewhat intimidating. But there is good news here: Working with Web technologies is remarkably similar to working with Office objects through VBA. Therefore, this chapter provides an introduction to using these Web technologies by building on your existing knowledge of VBA.
Note The sample files that accompany this chapter contain examples of the various tools and techniques discussed in this section and elsewhere in this chapter. To gain additional understanding of how the subjects discussed here apply to Office documents, you should examine the files in the ODETools\V9\Samples\OPG\Samples\CH12 subfolder on the Office 2000 Developer CD-ROM to see how they were built. This chapter also uses a number of the HTML sample files in the ODETools\V9\Samples\OPG\Samples\CH05 subfolder.
DHTML, through the document object model, provides an application programming interface (API) for working with HTML elements and cascading style sheet information. You can write script against the objects on a page to work with the data they contain as well as with their location and appearance. Writing script to manipulate the objects in a Web page is really no different from writing VBA code to manipulate the objects in an Office document.
The document object model supports a data-binding architecture that allows you to programmatically sort or filter data displayed on the page without requiring additional trips to the server. The document object model also supports Microsoft Scripting Components (scriptlets), which are lightweight, reusable objects that encapsulate code or a user-interface component. Scriptlets are created by using HTML code and script (either Microsoft Visual Basic Scripting Edition [VBScript] or Microsoft JScript).
To add script to an Office document, you use the Microsoft Script Editor. To open the Script Editor from inside an Office document, point to Macro on the Tools menu, and then click Microsoft Script Editor. For more information about working with the Script Editor, see "Scripting in Office Applications" later in this chapter.
VBScript vs. JScript
Although Microsoft Internet Explorer supports the use of VBScript code, JScript code, or both in a Web page, this chapter focuses primarily on working with VBScript code for three reasons:
VBScript is a subset of VBA, which means that you already know how to use this scripting language because of your familiarity with VBA. In many cases, you can paste VBA code directly into a Web page and have it run unchanged.
There is not room in this chapter to completely document either scripting language, so the discussion will focus on the one you are already familiar with.
VBScript is a lot easier to learn and a great deal easier to use than JScript. A simple example will illustrate why: JScript is a case-sensitive language and VBScript is not. If you are used to writing VBA code, you are used to writing If statements beginning with a capital "I." And if you inadvertently use a lowercase "I," the Visual Basic Editor capitalizes it for you. Since VBScript is case-insensitive, you can begin an If statement either way. JScript, on the other hand, requires an If statement begin with a lowercase "i." If you use a capital "I" in JScript, your script simply won't run. Debugging code is hard enough without having to worry about whether incorrect capitalization is causing a problem.
The only time you may not be able to use VBScript code in a Web page is for code used to detect the type of browser being used to view the page. VBScript code works in all versions of Internet Explorer 3.0 or later. However, VBScript code works in Netscape Navigator only by using a plug-in. Since JScript code runs in both browsers, you will want to write script that detects the browser type in JScript. To see an example of a scriptlet that uses a combination of VBScript and JScript code to return information about the browser being used to view a page, see the BrowserVersion.htm file in the ODETools\V9\Samples\OPG\Samples\CH12 subfolder on the Office 2000 Developer CD-ROM.
Note Many of the techniques discussed in this chapter involve technologies designed to work best with Internet Explorer version 4.01 or later. In particular, some of the DHTML features discussed in this chapter may not be fully functional when they are viewed in another browser. However, the DHTML features discussed here are designed to degrade gracefully when you use a browser that does not fully support all features. Typically, this means that interactive features are inoperable and the content is displayed as static data on the page.
A complete discussion of Web technologies is beyond the scope of this chapter, and these technologies are well documented in other resources. For a list of those resources, see "Where to Go from Here" at the end of this chapter.