Internet Explorer 5.0, Part II: Behavior Handler's Element Property - Doc JavaScript | WebReference

Internet Explorer 5.0, Part II: Behavior Handler's Element Property - Doc JavaScript


Behavior Handler's Element Property

The Behavior Handler has a single property, the element property. This property is read-only and returns the element that the specified behavior is applied to. The syntax for accessing the element property is as follows:

[ tmpElement = ] BehaviorID.element
where tmpElement is the accessed element and BehaviorID is the ID of the relevant Behavior, as specified in the Behavior-type Implements element of the behavior scriptlet. In our example, this statement reads as follows:

<IMPLEMENTS ID="kuku" TYPE="Behavior" DEFAULT>
and hence the element ID that this Behavior applies to is kuku.element. With this property, the Behavior Handler can communicate with the caller document.

All DHTML Object Model's properties, methods, and events are accessible via the Behavior's element property. The style property of the above element, for example, can be accessed as kuku.element.style, and, furthermore, the element position is kuku.element.style.position.

Luckily, there are a few shortcuts. First, you can designate a default behavior for the entire scriptlet, so instead of kuku.element, you can just refer to element. The default behavior assignment is done by specifying the DEFAULT attribute at the Behavior-type Implements tag:

<IMPLEMENTS ID="kuku" TYPE="Behavior" DEFAULT>

Moreover, even the element prefix can be omitted. Whenever a method, a property, or an event is referenced without any prefixes, it is assumed to belong to the element that the default behavior applies to. In our Connect Three game, we want to control the box appearance from the scriptlet, thus hiding the implementation details from the calling document. In particular, we set the position type, location, and source file for the box's GIF:

style.position = "absolute";
style.pixelTop = y;
style.pixelLeft = x;
src = "ibutton.bmp";
window.lastPlayedBy = "o";

Notice that we have also set the virtual last player to play with "O"-shape game pieces, by adding a user-defined property lastPlayedBy to the window object.

In our example, the following line use the same concepts of element hiding, when calling its methods:

window.attachEvent("onload", onload);
attachEvent("onclick", onclick);

The first line above applies to the window's object and therefore it is explicitly mentioned. The second attachEvent() method belongs to the element that the Behavior applies to. Following the above discussion on the default behavior and the default element, here are all three equivalent ways to call the element's attachEvent() method in our Connect Three game:

http://www.internet.com


Created: August 11, 1998
Revised: August 11, 1998

URL: http://www.webreference.com/js/column23/element.html