Using Object Properties and Methods in JavaScript

Using Object Properties and Methods in JavaScript

JavaScript programming centers on objects, to a large extent. We've already seen the document object and seen that one way to use that object is to use methods such as writeln to indicate that you want to write to a Web page. To use a method, you use the object's name , followed by a dot ( . ) and then the method name, such as document.writeln . Here are a few examples of methods:

  • document.write Writes to the body of the current Web page

  • document.writeln Writes to the body of the current Web page and ends the text with a carriage return

  • history.go Makes the browser navigate to a particular location in the browser's history list

  • window.alert Makes the browser display an alert dialog box

  • window. open Makes the browser open a new browser window, possibly displaying a new document

There are hundreds of such methods available in JavaScript, and they let you work with a browser as it's running. In addition to using methods to cause the browser to perform some action, you can read and change the settings in the JavaScript objects using properties. For example, the document.fgcolor property holds the color of text in the current Web page. By changing the document.fgcolor property, you can change the color of that text.

Here are some examples of properties, including the objects they belong to:

  • document. bgcolor Holds the background color of the current page.

  • document.fgcolor Holds the foreground (that is, default text) color of the current page.

  • document.lastmodified Holds the date the page was last modified (although many documents do not provide this information).

  • document.title Holds the title of the current page (which appears in the browser's title bar).

  • navigator.appName Holds the actual name of the browser, which you can use to determine what browser the user is using. We'll use this property at the end of the chapter to distinguish between Internet Explorer and Netscape Navigator.

Using object methods and properties, you have access to what's going on in a Web page, and you have complete programmatic control over the browser in many areas. We'll be putting methods and properties to work in this chapter and the next two chapters.

We've taken a look at the idea of methods and properties, but there's one more concept to cover before getting the actual programming details: using events in JavaScript. That topic is coming up.



Real World XML
Real World XML (2nd Edition)
ISBN: 0735712867
EAN: 2147483647
Year: 2005
Pages: 440
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net