167Chapter 14Document Object Model EssentialsObject MethodsAn object s (Most popular web site) method
167Chapter 14Document Object Model EssentialsObject MethodsAn object s method is a command that a script can give to that object. Some methods returnvalues, but that is not a prerequisite for a method. Also, not every object has methodsdefined for it. In a majority of cases, invoking a method from a script causes some action totake place. The resulting action may be obvious (such as resizing a window) or somethingmore subtle (such as sorting an array in memory). All methods have parentheses after them, and they always appear at the end of an object sreference. When a method accepts or requires parameters, the parameter values go insidethe parentheses (with multiple parameters separated by commas). While an object has its methods predefined by the object model, you can also assign one ormore additional methods to an object that already exists (that is, after its HTML is loadedinto the document). To do this, a script in the document (or in another window or frameaccessible by the document) must define a JavaScript function and then assign that functionto a new property name of the object. In the following example written to take advantage ofversion 4 or later browser features, the fullScreen()function invokes one windowobjectmethod and adjusts two windowobject properties. By assigning the function reference to thenew window.maximizeproperty, I define a maximize()method for the windowobject. Thus, a button s event handler can call that method directly. // define the functionfunction fullScreen() { this.moveTo(0,0); this.outerWidth = screen.availWidth; this.outerHeight = screen.availHeight; } // assign the function to a custom propertywindow.maximize = fullScreen; … A Note to Experienced Object-Oriented ProgrammersAlthough the basic object model hierarchy appears to have a class/subclass relationship, many ofthe traditional aspects of a true, object-oriented environment don t apply to the model. The orig- inal JavaScript document object hierarchy is a containmenthierarchy, not an inheritancehierar- chy. No object inherits properties or methods of an object higher up the chain. Nor is there anyautomatic message passing from object to object in any direction. Therefore, you cannot invokea window s method by sending a message to it via the documentor a form object. All object ref- erences must be explicit. Predefined document objects are generated only when the HTML code containing their defini- tions loads into the browser. You cannot modify many properties, methods, and event handlersin early object models once you load the document into the browser. In Chapter 33, you learnhow to create your own objects, but those objects do not present new visual elements on thepage that go beyond what HTML, Java applets, and plug-ins can portray. Inheritance doesplay a role, as you will see later in this chapter, in the object model defined bythe W3C. The new hierarchy is of a more general nature to accommodate requirements of XMLas well as HTML. But the containment hierarchy for HTML objects, as described in this section, isstill valid in W3C DOM-compatible browsers.