|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Using Built-In Objects and Functions Individual JavaScript elements are objects. String literals are string objects, for example, and they have methods that you can use to change their case, and so on. JavaScript can also use the objects that represent the Web browser in which it is executing, the currently displayed page, and other elements of the browsing session. To access an object, you specify its name. Consider, for example, an active document object named document. To use documents properties or methods, you add a period (.) and the name of the method or property you want. For example, document.title is the title property of the document object, and explorer.length calls the length member of the string object named explorer. Remember, literals are objects too. You can find out more about the objects built in to JavaScript and the Web browser in the next chapter. Using Properties Every object has properties, even literals. To access a property, use the object name followed by a period and the property name. To get the length of a string object named address, you can write the following: address.length You get back an integer that equals the number of characters in the string. If the object you use has properties that can be modified, you can change them in the same way. To set the color property of a house object, for example, use the following line: house.color = blue You can also create new properties for an object simply by naming them. If you define a class called customer for one of your pages, for example, you can add new properties to the customer object as follows: customer.name = Joe Smith customer.address = 123 Elm Street customer.zip = 90210 Finally, knowing that an objects methods are properties is important. You can easily add new methods to an object by writing your own function and creating a new object property using your own function name. If you want to add a Bill method to your customer object, you can do so by writing a function named BillCustomer and setting the objects property as follows: customer.Bill = BillCustomer; To call the new method, you use the following: customer.Bill() Array and Object Properties JavaScript objects store their properties in an internal table that you can access in two ways. You have already seen the first wayjust use the properties names. The second way, arrays, enables you to access all an objects properties in sequence. The following function prints out all the properties of the specified object: function DumpProperties(obj, obj_name) { result = // set the result string to blank for (i in obj) result += obj_name + . + i + = + obj[i] + \n return result } You can access all the properties of the document object, for instance, both by property nameusing the dot operator (for example, document.href)and by the objects property array (document[1], although this may not be the same property as document.href). JavaScript provides another method of array access that combines the two: associative arrays. An associative array associates a left- and a right-side element; the value of the right side can be used by specifying the value of the left side as the index. JavaScript sets up objects as associative arrays with the property names as the left side and their values as the right. You can, therefore, access the href property of the document object by using document[href]. Programming with JavaScriptJavaScript has a lot to offer page authors. It is not as flexible as C or C++, but it is quick and simple. Most importantly, its easily embedded in your Web pages; thus, you can maximize their impact with a little JavaScript seasoning. This section covers the gritty details of JavaScript programming and includes a detailed explanation of the languages features. ExpressionsAn expression is anything that can be evaluated to get a single value. Expressions can contain string or numeric literals, variables, operators, and other expressions, and they can range from simple to quite complex. The following are examples of expressions that use the assignment operator (more on operators in the next section) to assign numeric or string values to variables: x = 7; str = Hello, World!; In contrast, the following is a more complex expression whose final value depends on the values of the quitFlag and formComplete variables: (quitFlag == TRUE) & (formComplete == FALSE) OperatorsOperators do just what their name suggests: They operate on variables or literals. The items that an operator acts on are called its operands. Operators come in the following two types:
Assignment Operators Assignment operators take the result of an expression and assign it to a variable. JavaScript doesnt enable you to assign the result of an expression to a literal. One feature of JavaScript not found in most other programming languages is that you can change a variables type on-the-fly. Consider the HTML document shown in Listing 18.1.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |