spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column23


Behavior Handler's Element Property

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

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:

  • attachEvent()
  • element.attachEvent()
  • kuku.element.attachEvent()

http://www.internet.com

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint


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

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