Reading Properties and Executing Methods

In the last few pages, we’ve concentrated on the different methods that we can use to connect code with events fired by objects on a page. However we've stayed simple as the only code we’ve been executing is a simple routine that displays a message box on the screen. If that’s all you ever want to do, then you can get by with skipping the rest of this chapter. If, however, you’re like the rest of us, and need to do other things with your code, then you’ll be interested in this section which shows how code executes the methods of different objects, and how you can read and modify an object's properties.

Switch to or open up NotePad again, and enter this new code:

<HTML>
<HEAD>
<TITLE> Methods And Properties </TITLE>
</HEAD>
<BODY>
<CENTER>
<H1> Using Methods and Properties </H1>
<FORM NAME="frmTest">
New URL:
<INPUT TYPE="TEXT"  NAME="txtURL"><p>
<INPUT TYPE="BUTTON" NAME="btnURL" VALUE="Change URL">
</FORM>
<SCRIPT LANGUAGE="VBScript">
Sub btnURL_onClick
Dim newURL
newURL = Window.Document.frmTest.txtURL.Value
Window.Alert "The current URL is " & Window.Location
Window.Alert "The new URL will be " & newURL
Window.Navigate newURL
End Sub
</SCRIPT>
</CENTER>
</BODY>
</HTML>

Next load the page into Internet Explorer:

If you enter a valid URL into the text box and press the Change URL button, then you'll get a message box displaying the current URL. If you click OK to dismiss this first message box, another message box appears which displays the URL entered in the text box. After clicking OK a second time, the browser loads the page specified by the URL we entered, and displays it.

The code that does this looks a little more complicated than last time, but it’s quite logical—and it shows many things we’re interested in learning about. Specifically, the code on this page reads properties and calls a method, and—as a bonus—it even uses a variable to do some of its work.

How It Works

Since we’ve covered how to hook up code to events, we’re now free to focus just on the code inside the <SCRIPT> block. We’ll talk about each section in turn.

Dim newURL

The Dim newURL line declares a variable named newURL, which will be valid for the duration of this subroutine execution. This line isn’t absolutely necessary, because VBScript doesn’t require variable declaration. However, it’s always good practice and makes the code we write easier to read and understand.

It’s the next line that’s more interesting. The horrendously long string starting with Window is actually just a way to access the property we’re interested in, which in this case is the value inside the text box on the form:

newURL = Window.Document.frmTest.txtURL.Value

Generally, accessing properties (to read or write to them) is just accomplished by using this syntax:

Assigning to a variable VariableName = ObjectName.PropertyName
Setting a property ObjectName.PropertyName = VariableName

In our case, we actually have to access three additional properties in succession, before we get to Value, the one we’re interested in. Document is a property of the default Window object, frmTest is a property of the Document object (because in our HTML we created a form in the document called frmTest), and txtURL is a property of the frmTest object. Finally, after this long string, we get to what we want. Don’t worry about understanding why exactly we needed to use Document, frmTest, or txtURL here, it's more important to understand how we set and retrieve properties in general. It shouldn’t be too hard – it’s identical to how it’s done in server-side code.

Specifying the Default Window Object

In this example, we always enter the Window object wherever it applies. This isn’t strictly necessary, because the Window object is the default object when we write client-side code. Look at the two lines of code below:

Alert "Hello World!"
Window.Alert "Hello World!"

Both of these lines accomplish the same thing, but the first line doesn’t bother to say Window. This is fine, because Window is always assumed if it’s not entered. In most of our examples, we’ll be taking the shortcut and not specifying Window if it’s not necessary.

The next two lines call our old friend the Alert method, displaying a message box with the current location and then a message box with the location we’re jumping to.

Window.Alert "The current URL is " & Window.Location
Window.Alert "The new URL will be " & newURL

The current location comes from the Location property of the Window object, and our new location is stored in the newURL variable we set earlier. We just concatenate these two values into strings with some descriptive text, and display them in their own message boxes.

Navigating to a New URL

Navigate is a method of the Window object. It takes a URL as a parameter, and changes the current location to that URL:

Window.Navigate newURL

This example just shows how we can call a method of an object using VBScript. It’s simple: use the same ObjectName.MethodName syntax like we would in ASP on the server.

So we’ve covered how our scripts are located in HTML, how they’re connected to events, and how methods and properties are accessed. We’re now ready to get into the second major part of this chapter and talk about how to control the browser itself.

© 1997 by Wrox Press. All rights reserved.