Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Schongar P.VBScript unleashed.1997

.pdf
Скачиваний:
46
Добавлен:
23.08.2013
Размер:
1.59 Mб
Скачать

30:<INPUT TYPE="BUTTON" NAME="Button2" VALUE="Set fgColor">

31:<INPUT TYPE="BUTTON" NAME="Button3" VALUE="Set Message">

32:<INPUT TYPE="BUTTON" NAME="Button4" VALUE="Read Cookies">

33:<BR><BR>

34:<H4>Click <A HREF=index2.htm>here</A> to read the cookies from another page

</H4>

35:</CENTER>

36:</BODY>

37:</HTML>

The first three Command buttons on this page create three variables and store them in the cookie. The names of the variables are FGCOLOR, BGCOLOR, and MESSAGE. Their values are specified by the user with the help of an InputBox() function. You can set the value of each cookie as many times as you want, and the cookie property isn't going to create additional variables. Each time a new value is assigned to an existing variable, it replaces the existing value and does not create a new variable.

The destination of the hyperlink is the Index2.htm file, whose contents are shown in Listing 14.2.

Listing 14.2. The Index2.htm file.

1:<HTML>

2:<HEAD>

3:<TITLE>Setting and Reading Cookies</TITLE>

4:<HEAD>

5:<SCRIPT LANGUAGE=VBS>

6:Dim cookies(2,50)

7:

8:Function GetCookie(whichVariable)

9:cookie=Document.Cookie

10:VarPosition=Instr(cookie, whichVariable)

11:If VarPosition=0 then

12:GetCookie=0

13:Exit Function

14:Else

15:StartVarPosition=VarPosition+Len(whichVariable)

16:EndVarPosition=Instr(StartVarPosition, cookie, ";")

17:If EndVarPosition=0 then EndVarPosition=Len(cookie)+1

18:GetCookie=Mid(cookie, StartVarPosition, EndVarPosition-

StartVarPosition)

19:End If

20:End Function

21:

22:Sub CButton1_onClick()

23:ckVar=InputBox("Enter the name of the cookie")

24:If ckVar<>"" Then

25:ckVal=GetCookie(ckVar)

26:End If

27:If ckVal<>"" Then

28:msgbox "The value of the " & ckVar & cookie & " is " &

ckVal

29:Else

30:msgbox "No " & ckVar & "cookie found"

31:End If

32:End Sub

33:

34:Sub CButton2_onClick()

35:cookie=Document.cookie

36:i=1

37:While len(cookie)>0

38: pos=Instr(cookie, "=")

39:If pos>0 then

40:

ckVar=left(cookie, pos-1)

41:

cookie=mid(cookie,pos+1)

42:

pos=Instr(cookie,";")

43:

If pos=0 then

44:

ckVal=cookie

45:

cookie=""

46:

Else

47:

ckVal=left(cookie,pos-1)

48:

cookie=mid(cookie, pos+1)

49:

End If

50:

cookies(1,i)=ckVar

51:

cookies(2,i)=ckVal

52:End If

53:i=i+1

54:Wend

55:ncookies=i-1

57:Document.open

58:Document.write "There were " & ncookies & " cookies in the document"

59:Document.write "<TABLE>"

60:For ck=1 to ncookies

61:ckvar=cookies(1,ck)

62:ckval=cookies(2,ck)

63:Document.write "<TR>"

64:Document.write "<TD>" & ckvar & "<TD>" & ckval

65:Document.write "</TR>"

66:Next

67:Document.write "</TABLE>"

68:Document.close

69:End Sub

70:</SCRIPT>

71:<BR><BR>

72:<INPUT TYPE=BUTTON NAME=CButton1 VALUE="Show A cookie">

73:<INPUT TYPE=BUTTON NAME=CButton2 VALUE="Show All cookies">

74:<A HREF=index.htm>Back to previous page</A>

75:</BODY>

76:</HTML>

The GetCookie() function accepts the name of the cookie to extract as argument and extracts it from the Document.cookie property. First, it locates the variable's name in the cookie, and then it looks for the following semicolon. When GetCookie() finds the variable, it has in effect located the value following the variable name and returns it to the calling function. VarPosition is the position of the variable name in the cookie. After the variable name is found, the function looks in the rest of the cookie to find the following semicolon, which signals the end of the cookie's value (or the end of the string, if it happens to be the last cookie in the string). The StartVarPosition and EndVarPosition variables delimit the cookie's value in the string, and they are used to extract it.

The CButton2_onClick() subroutine works in a very similar manner. It starts at the left end of the string and treats everything to the first equal sign as the variable name. Then it locates the following semicolon, and everything between the equal sign and the semicolon is the value of the variable. With the first variable/value pair out of the way, it truncates the cookie by removing everything from the left up to (and including) the semicolon, and it repeats the same process over and over until the entire cookie has been searched.

Review

The customization techniques you learned in this chapter rely on the document itself and the browser. To create a highly customizable Web, like the MSN Web site for example, you must do some programming on the server. Let's say you have a number of home pages, each one for a specific category of users-a page for sports fans, another one for music lovers, another one for computer game addicts, and so on. To display the appropriate page to every client who has customized his or her home page, prepare a starting page with no content, or a simple design that informs the user that his or her custom page is being downloaded. This generic page should contain the VBScript code for grabbing the values of the cookies, place them in a hidden TextBox control, and then submit them to the server with the Form's Submit method. Or decide which of the many start pages suits the user's interest the best and request that page. If the cookie contains the string sports, the CustomSports page should be loaded. One way to do this is via the href property of the document's location object.

A customized site such as the MSN site uses a CGI application that creates the custom home page on-the-fly. Again, you can start with a generic home page, which reads the value of the cookie with all the settings, stores them in a hidden TextBox, and submits its contents to the server. The server will then generate a new home page on-the-fly, based on the viewer's preferences. There's quite a bit of overhead with this approach, because generating good-looking pages programmatically requires good programming skills and creativity. To simplify the task of setting up a custom page, you

can use frames, some of which will be the same on all pages and the remaining ones will contain customized content. Frames will help you organize the information you want to place on the pages, without having to worry about sizing the pages and the positioning of the elements on them. The users will decide how big each frame will be.

The last approach of combining cookies and CGI applications to customize your Web is the most flexible approach, but the most demanding one in terms of authoring. If your goal, however, is to attract users to your site, this is the extra step that will place your site ahead of the others. You can also use this technique quite efficiently in an intranet environment to provide custom pages to certain groups of users. The users in the marketing department, for instance, will see the Marketing.htm page every time they start Internet Explorer, and users in the accounting department will see the

Accounting.htm page.

Chapter 6

The Scripting Model

by Evangelos Petroutsos

CONTENTS

Windows, Documents, and Frames

The window Object's Properties

Methods

The document Object

The document Object's Properties

The document Object's Methods

Using the document Properties and Methods

A Self-Modifying Document

Color Cycling

A Yearly Calendar

The history Object

The HistoryObject Project

The navigator Object

The BrowserInfo Project

The NavigatorObject Project

The location Object

The LocationObject Project

The link Object

The LinksObject Project

Review

In the previous chapters, you learned how to write applications with VBScripts and how to program the controls to activate your pages. VBScript can also help control the browser's window, which is the container where your pages get displayed and your code is executed. In this chapter, you explore the objects that let you control the environment in which your VBScript applications will be executed. In terms of traditional programming, you've learned the language. In this section, you learn how to program the environment of the application, too.

ActiveX controls, the objects you use to build the interface of the application, are manipulated through their properties and methods. Internet Explorer provides another set of objects, with their own properties and methods. These objects enable you to manipulate the environment of Internet Explorer itself. Because Internet Explorer is the operating system for your VBScript applications, you can think of these objects as the operating system's controls. The browser's window is such an object. So far, this window has served as nothing more than the container for your application. It was the environment in which VBScript applications were executed. Now, you learn how to control the window object programmatically. You will see that it is possible to load another document in the browser from within your code; it's even possible to create pages on the fly with VBScript code. You will also learn how to control some of the browser's functionality, such as moving back and forward through the history list from within your applications. This process is equivalent to clicking the Back and Forward buttons on your browser, but you'll be able to perform these actions from within your VBScript application.

The material of this chapter assumes a basic knowledge of HTML authoring. You should understand the structure of an HTML document and the HTML tags used in the examples and their attributes. Some topics that are fairly new to Web authoring, such as floating frames, will be explained in the course of the chapter. To make the best of the HTML object model, however, you should at least have a familiarity with HTML authoring.

Windows, Documents, and Frames

thisURL=window.document.location.href

As usual, you can omit the window identifier. Even so, you have a rather lengthy statement, but don't let it intimidate you. The URL is a location property and the location object applies to the document object. Thus, the location is a property of the document, and not the window. href is a property of the location object, which in turn is a property of the document object, which in turn is a property of the window object. You must write down a sequence of objects, starting with the top level object (the window object) and work your way down to the lower-level object whose property you want to access.

A window can also contain frames, which you can access through the frames object. The frames object is an array of objects, the first frame being frames(0), the second one frames(1), and so on. To control the background color of the document in the first frame, you must access the bgColor property of the document of the first frame. Each frame has its own document property similar to the window object. This time we want to access the document of a frame, which in turn is a property of the window object. Instead of getting the window.document.bgColor property as you saw earlier, you must access the document.bgColor of the frames(0) property of the window object:

window.frames(0).document.bgColor

Likewise, you can specify the URL of a document to be displayed in a frame. You start with the href property of the location object, which now must be applied to a frame, and not the window's document. To cause another document to be displayed in the first frame of the window, you must use a statement like:

window.frames(0).location.href

As you can see, the same object can be attached to multiple containers (which are also objects). The window has its own document object, and the document has a location property. If the window contains frames, however, each frame in the window has its own location property. You may find this behavior confusing at first, but you will soon get the hang of it.

In Microsoft's colorful terminology, each entity you see on the browser's window is an object. Objects "expose" some of their properties, which enables you to manipulate them. Some of these properties represent objects themselves, which expose their own properties. The window object exposes the name property, which enables you to examine the name of a window from within your code. To manipulate the background color of the document on the window, you must first access the document property of the window (window.document) and then the bgColor property of the document object (window.document.bgColor).

In the following sections, you explore the objects of the scripting model and their properties, as well as their methods, which enable you to control the Internet Explorer's environment from within your code. The text begins with the window object and one of its most important properties, the document object. As you will realize, the document object offers tremendous flexibility over the appearance and function of your Web pages, and it appears in many examples for this section.

The following list provides the other objects of the window object, which you will explore in this chapter:

history

history is an object you can use to access the history list of the

 

current window. This is a list of the URLs visited already.

navigator

The navigator object contains information about the browser, such

 

as the name of the browser and its version.

location

The location object provides information about the window's

 

current URL.

document

The most important object of all. It's the actual document in the

 

current window.

The window Object's Properties

The following paragraphs explain the various properties of the window object and offer short examples that

demonstrate the syntax of the various properties. In the sections that describe the other objects of the scripting model, you will find more elaborate examples of the various properties of the window object's properties.

name

name is a read-only property that returns the name of the window. To create a named window, you must use the TARGET attribute with one of the HTML tags to which it applies. For example, the <A> tag is frequently used with the TARGET attribute, which tells the browser to display the destination HTML document in another window. The HTML line

Click <A HREF="http://www.microsoft.com">here</A> to visit the Microsoft Web site.

generates a hyperlink, which displays the root document of Microsoft's Web site when activated in the same window that contains the hyperlink. If you specify the TARGET attribute, however, the same document will appear in a separate window:

Click <A HREF="http://www.microsoft.com" target="MS">here</A> to visit the Microsoft Web site.

If a window with the name MS exists already, the document will be displayed in it. If such a window doesn't exist, a new window opens and the document appears there. You can later refer to this window by its name. For example, you can set the string to appear in the window's status bar using the status property.

parent

The parent property returns the name of the parent window, which contains the specific window. The parent property of the window object is an object itself, and through its properties, you can adjust the appearance of the parent window.

opener

The opener property returns the window object that opened the current window. The opener property does not return a window name or another window property. It returns an actual object that you can use to access the properties of the window from which the current window was opened. To find the name of the opener window, use the Name property, as in the following line:

MsgBox "My parent window is " & opener.name

self

self is a window object that represents the current window, but, although mentioned in Microsoft's documentation, it's not yet supported by Internet Explorer 3.0.This property operates similarly to the parent and opener properties, in the sense that it returns a window object, and not the name of the window. To find out the status message of the current window, you could use the status property of the self object:

If self.status = "VBScript" Then

self.status = "ActiveX"

Else

self.status = "VBScript"

End If

If you place this If structure in a Timer event's handler, the message on the browser's status bar would alternate between the strings VBScript and ActiveX.

top

The top property returns an object that represents the topmost window.

location

The location property returns another object, the location object, which is described in detail in a later section. The most important property of the location object is the href property, which returns or sets the URL of the current window. The statement

MsgBox window.location.href

displays the URL of the current window in a message box. To activate another URL from within your code (in other words, without relying on the user to click on a hyperlink, or the browser's Back button), use a statement such as:

window.location.href="www.microsoft.com"

defaultStatus

This property sets the default text in the status bar. See the description of the status property to find out how you can display a message in the browser's status bar.

status

The status property returns or sets the text displayed in the status bar. The statement

window.status = Now

displays in the status bar the date and time the document was opened. To update the status bar, use the previous statement form within a Timer event handler. Figure 6.1 shows two windows with different status messages. The two status messages were displayed with the statements

Figure 6.1 : With the status property of the window object, you can control the message displayed in the browser window status bar.

window.status="Welcome to SAMS Publishing"

and

window.status="Today's date is " & date & " and the time is " & time

frames

This property returns an array of objects, which are the frames in the current window. Use it to access the various properties of a frame object, such as its name or its URL: