JavaScript Is Object-Based

JavaScript is an object-based language. The term object-based should not make you nervous: For us, object-based programming will be a lot easier. For our purposes, object-based programming just means that JavaScript makes available objects that give us access to some aspect of the browser or document.

For example, we already have used the document object, one of the most powerful JavaScript objects, in this chapter. That object refers to the body of the Web page in the browser. With this object, you have access to the HTML in the page. In the previous example, I used the writeln (which stands for "write line") method of the document object to write "Welcome to JavaScript!" , like this:

 <SCRIPT LANGUAGE="JavaScript">  <!--  document.writeln("Welcome to JavaScript!")  //--> </SCRIPT> 

You can use methods such as writeln to have an object perform some action, such as writing to the Web page. Other methods let you force the browser to navigate to a new page, send data back to scripts on the server, and so on.

The objects that you already have access to in JavaScript give you a great deal of power. For example, we'll use the document object to access XML documents in the next chapter. Here's an overview of some of the most popular JavaScript objects and what they're all about:

  • document Represents the current Web page's body. With this object, you can access all the elements in a Web page, such as links, images, anchors, and so on.

  • history Has a record of what sites the Web browser has been to before opening the current page. Using the methods of this object, you can move backward and forward though the history list.

  • location Holds information about the location of the current Web page, including its URL, domain name , path , server port, and so on.

  • navigator Actually refers to the Web browser itself. Using this object, you can determine the type of browser in use.

  • window Refers to the current browser window and provides many powerful methods. In Internet Explorer, you use the event subobject of this object to handle events, as we'll see at the end of the chapter.

Many more objects exist in JavaScript, and you can define your own objectssee the JavaScript documentation for the details. When you want to create your own objects, you first define a class, which you can consider the type of the object. Using the JavaScript new operator, you can create objects from classes; we won't create classes here, but we'll use the new operator later in this chapter to create objects in the next chapter.

You have to know about two aspects of objects to be able to get anywhere methods, which we've already seen, and properties.



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