Behavior Code

Behavior Code

y now I am sure that many Visual Basic developers are leveraging their Visual Basic skills to do some Web development. In this Solution, I’ll cover DHTML Behaviors, a new feature of Internet Explorer 5 that can be used in a Visual Basic DHTML application, Browser control, or HTML pages. (Just remember that it’s IE5-specific.) I will introduce the concepts of a Behavior from a Visual Basic developer’s perspective and help you further leverage your skills into this new and cool technology.

Behaviors allow you to add custom interfaces to any Element of an HTML document. The custom interface can include properties, methods, events, and event handlers. Once the Behavior is attached to an Element, the Behavior’s interface blends seamlessly with the Element’s standard interface. So an HTML page author can manipulate the Elements custom interface through script with the standard Element.Method syntax. A Behavior can control the element that it is attached to. It can also control the entire page through the DHTML object model’s top-level Window object. First I’ll explain how to create a Behavior and then I’ll discuss how to attach it to an Element.

Creating a Behavior
A behavior is not written within the HTML page, but rather is stored in a separate file with an .htc extension. The Behavior is structured in a tagged format. The entire Behavior is enclosed in tags. Following the opening tag is the Declarations section, in which you must declare any procedures that will be part of the Behavior’s public interface. After the Declarations section is the scripting section which is enclosed in < /PUBLIC:COMPONENT>

There are four types of procedure declarations:

This declaration is used for methods and must have a matching procedure within the script tags. The Name attribute should be the name of the matching procedure.

This declaration is used for properties, and must have two procedures: one for the Property Let and one for the Property Get. The Get attribute should be the name of the Get procedure and the Put attribute should be the name of the Let procedure. The procedures do not have to be prefixed with let, get, or put.

This declaration is used to declare events. A procedure within the Behavior must raise the event in order for it to fire. The syntax for raising an event is the Event's ID attribute followed by fire, e.g.

This declaration is used to allow the Behavior to respond to events within the document. In the Event tag, specify the event that you want to handle. In the For attribute, specify the object which raises the event, this may be either the Element (attached to the Behavior), Window, or Document objects. The OnEvent attribute should be the name of the function within the behavior that handles the event.

Attaching a Behavior
Behaviors are integrated with HTML Styles, so that you can attach a behavior through a style. The syntax is:


To attach a behavior stored in a file named to a Link Element using an inline style, you would use the following syntax:


If the behavior is not in the same directory as the HTML page, then specify the directory path.

Sample Behavior
Here's a sample Behavior that allows an Element to hide or show its child elements. To see it in action (if you are running IE5) click here.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist