The scripting objects also have methods. The Document object, for example, provides the Write method, which lets your script place text directly on the Web page. In other words, with the Write method you can create Web pages on the fly. The Write method displays a string on the current page. The following statement:
Document.Write Date()
displays the current date on the page. If you use HTML instead, you must hardcode the date and consequently update the document daily. The VBScript Datet) function returns the current date, but VBScript doesn’t provide any methods for actually displaying the date on the page. To display something on a page from within its script, you use the objects of the Scripting Model.
Let’s look at an example. Here’s a simple HlML document:
<1frML>
<BODY BGCOlOR-‘#HOOFFOO’>
<Hl>Welcome to Visual Basic and the Web</Hl>
</BODY>
</HTMl>
This document displays a page with a green background and a level 1 heading. You can create the same page with the following VBScript code:
<HTML>
<SCRIPT lANGUAGE= VBScript’>
Document.bgColor = ‘#HOOFFOO’
Document.Write ‘<Hl> Welcome to Visual Basic and the Web</Hl>’
</SCRIPT>
</HTMl>
What’s the benefit of using the Write method to generate the page? Flexibility. This page is actually generated on the client computer. If you want to display the date and the time this page was opened, you can add the following line of VBScript code:
The Write method provides even more flexibility.You can write complicated VBScript code to produce elaborate pages on the fly. For example, you can prompt the user’ . for his or her name and personalize a Web page as follows:
The actual-heading will be different on each client computer, depending on the . user’s response to the prompt. Figure 21.7 shows a typical page generated on the fly with VBScript code that manipulates the IE Scripting Objects
Notice that this document doesn’t contain any HfML tags in its BODY section. The entire document was generated from within the page’s script section with VBScript commands.
In the following sections, we are going to explore the Document object of the Scripting Model, since this is the most important one from a VBprogrammer’s point of view.
The Document Object
From a programming point of view, the Document object is probably the most important
object in the scripting hierarchy. The Document object represents the HI’MLdocument displayed in the browser’s window or in one of its frames. Through the Document object’s properties and methods, you can manipulate the appearance and even the contents of the document. You can use the bgColor property, for example, – . to read or set the document’s background color, and you can use the TItle property to read the document’s title. You use its Write method to specify the document’s contents from within the script and, in effect, create documents on the fly.The following section explains the properties of the Document object and provides short examples
that demonstrate the syntax of the propeties.
The Properties of the Document Object
The Document object provides a few simple properties that let you set the document’s background color, the color of the links, and so on. It also provides a few of the most advanced properties, such as the Cookie property, which lets your script store information on the client computer and read it the next time the document with the ‘script is loaded.
IInkColor, aUnkColor, vUnkColor These properties return or set the color of the links in the document. The linkColor property’ is the default color of the hyperlinks in the document, aLinkColor is the color of the active hyperlink, and vLinkColor is the color of the hyperlinks already visited. These properties accept color values that can be expressed- as hexadecimal numbers or as color names:
bgColor, fgColor These properties return or set the document’s background color and foreground color. The foreground color is the color used for rendering text if the HTML code doesn’t overwrite this setting. Likewise, the background property can be overwritten by the document if it uses a background image. These properties accept color values.
Title This property returns the current document’s title. This is a read-only property and can’t be used to change the document’s title at runtime.
Cookie As you know, scripts written in VBScript are executed on the client com-> puter. VBScrlpt, therefore, had to be a safe language. There is no way for VBScript to access the file system of the client computer and tamper with it. That’s why VBScript lacks the file I/O commands of VISUal Basic.A language that can’t store information locally is rather limited. Scripts can’t even open or save dew bytes of data on a local file, and for many applications, this is a serious limitation.
The solution to this problem is to use cookies. A cookie is a property of the Document object and is a string that can be stored on the client computer. Cookies are quite safe, though, because they are text files written and read to and from. the disk by the browser, and they live in a specific folder. They are not executable files (they present no threat to the rest of the file system), and they can be accessed only by the browser. Cookies can’t be considered a substitute for file 1/0, but they can save a piect¥,cifinformation on the client computer so that the next time the script is executed, irWin find the information there. The information stored on the client computer by means of cookies is limited. You can’t store large files with text or numbers. But you can store customization information such as the user’s name and preferences so that the-next time the user requests the same page, the script can find the values it stored on the client . computer the last time and customize itself for the user.
Another practical reason for using cookies is to share information among pages. The shopping basket is a typical example. As you know, a script is limited to a single page. If the page with the script loads another page, the original script ceases to exist. The script (if any) on the newly loaded page takes over. Some sites let viewers select items to purchase on various pages, and they keep track of the items in the user’s shopping basket. If each page is a separate entity and the pages can’t share information, how is this done?
The answer is the Cookie property of the Document object. When a page wants to pass some information to other pages, it can leave a cookie on the client computer. The page that needs the information can read it. To the viewer, it appears that the various pages are communicating as if they were Forms of an application, to use a Visual Basic analogy.