December 24, 1999 - Passing Objects to Behaviors | WebReference

December 24, 1999 - Passing Objects to Behaviors

Yehuda Shiran December 24, 1999
Passing Objects to Behaviors
Tips: December 1999

Yehuda Shiran, Ph.D.
Doc JavaScript

A Behavior operates on an object that is associated with a DHTML element. In our Blinking Soccer example from Column 22, DHTML Behaviors, we first define a Behavior named soccer in the scriptlet file, and then, by assigning it to the CLASS attribute, we associate a DIV element with this Behavior:

<DIV CLASS="soccer" ...

Once a Behavior is associated with a DHTML element, any operation inside the Behavior definition script is assumed to operate on the DHTML element's object. When you manipulate properties inside the soccer Behavior, for example, you actually manipulate the properties of the relevant DHTML object. The following lines from the soccer scriptlet change the visibility and position of the DIV object in the Web page:

style.position = "absolute";
style.pixelTop = x;
style.pixelLeft = y;

Referencing an object's methods is more complicated. Internet Explorer 5.0 does not default to the relevant DHTML object. Instead, it provides you with the uniqueID mechanism. The uniqueID is a unique ID of the DHTML object that is currently being operated by the Behavior. When we want to use the blink() method in our Blinking Soccer example, we call it as uniqueID+".blink()". When we want to schedule the blinking msecs milliseconds in the future, we use:

window.setTimeout(uniqueID+".blink()", msecs);