Behavioral extensions provide a standard way for declaration of standalone behaviors that can be attached to any HTML or XML element, without modification of the DTD. Partly based on extensions to Cascading Style Sheets (CSS), these behaviors can be applied to multiple documents using the existing CSS linking mechanisms.
A D V E R T I S E M E N T
Through XML we can link behaviors to any element in a web page and manipulate that element.
DHTML are using a CSS attribute called "behavior" in place of <script> tag.This "behavior" specifies a URL to an HTC file which contains the actual behavior (The HTC file is written in XML)
Syntax
behavior: url(some_filename.htc)
Note: The behavior attribute is only supported by IE 5
and higher. All other browsers will ignore it, means that
Mozilla, Firefox, Netscape and other browsers will only see the
regular content and IE 5+ can see the DHTML behaviors.
Benefits:
DHTML behaviors makes things easier for everyone involved in the Web development process and add great value to a Web application environment.
In the real world, this environment consists of a team of content authors, designers, and developers. Content authors are responsible for writing content. Designers determine what interactive effects can be added to the content, while developers implement those effects.
The following section outlines the advantages and benifits by using behaviors.
Behaviors Provide a Means for code reuse and Script Encapsulation
Behaviors allow Designers to Add Interactive Effects with a Simple Declarative Syntax
Behaviors Isolate Script from Content, Resulting in more manageable and Cleaner Pages
Behaviors engross Easy-to-Create Components
Behaviors Provide a Means for code reuse and Script Encapsulation
With behaviors,it is very easy to add interactive effects as encapsulated components that can be reused across multiple pages. For example, consider one of the more popular effects made possible in Internet Explorer 4.0: onmouseover highlights. Through the use of CSS rules, and the ability to change styles on the fly, it is easy to achieve this effect on a page. In Internet Explorer 4.0, the way to implement onmouseover highlights on a list item, or li, would be to handle the onmouseover and onmouseout events in this manner:
Beginning with Internet Explorer 5, a DHTML behavior can be implemented to achieve this effect. This behavior, when applied to an li element, extends the list item's default behavior to change its color when the user moves the mouse over it.
The following example implements a behavior in the form of an HTML Component (HTC) file, which is contained in the hilite.htc file, to achieve the onmouseover highlight effect. The behavior is applied to the li with the familiar style block, using the proposed CSSbehavior attribute that specifies the location of the behavior. With the behavior applied, the preceding code can look something like this in Internet Explorer 5 and later:
<HEAD>
<STYLE>
LI {behavior:url(hilite.htc)}
</STYLE>
</HEAD>
<BODY>
<UL>
<LI>HTML Authoring</LI>
</UL>
</BODY>
Behaviors allow Designers to Add Interactive Effects with a Simple Declarative Syntax
Currently, the task of adding interactive effects to a page can involve a lengthy iterative process between the designer and the developer trying to give the page that perfect look. The designer, usually with limited programming background, mocks up the desired effect on the content in a desktop publishing environment, and works closely with the developer to achieve the same effect on the page, usually with Dynamic HTML.
With behaviors, a developer can work independently on encapsulating the desired effect in a separate file, while the designer applies that behavior to elements on the page with a few CSS attributes. By providing a simple declarative syntax, requiring no prerequisite knowledge of scripting and DHTML, behaviors empower Web designers to easily add interactive effects to an otherwise static content. As seen in the preceding example, adding an onmouseover highlighting effect on a list item is as easy as adding the familiar style block, in exactly the same way a style is attached to an element through CSS.
Behaviors Isolate Script from Content, Resulting in more manageable and Cleaner Pages
Behaviors provide easy partition of script from contents, as it
moves all script contained in a page into a separate file. The
previous example demonstrated how the script to handle the
onmouseover and onmouseout events was moved to a separate
file, hilite.htc. This example can be extended to implement
hiding content and display it, making it even easier to see how
behaviors can make a difference, and how script isolation results in
a cleaner, script-free page.
The following example demonstrates
the use of the two effects, onmouseover highlight and
displaying/hiding content, in a table of contents scenario. The same
example has been implemented two different ways:
For Internet Explorer 5.0 & later, using Dynamic HTML
Using DHTML behaviors, the code to implement the same table of contents looks a lot cleaner, as most of the script is isolated into a separate file.
<HEAD>
<STYLE>
CollapsingAndHiliting {behavior:url(ul.htc) url(hilite.htc))}
A {behavior:url(hilite.htc)}
</STYLE>
</HEAD>
HTC files provide the easiest and quickest way to create DHTML behaviors using scripting languages such as Microsoft Visual Basic Scripting Edition (VBScript) and Microsoft JScript (compatible with ECMA 262 language specification). However, like any component used on the Internet today, behaviors implemented using Microsoft Windows Script Component (WSC) or Microsoft Visual C++.
The behavior used in the previous example to apply onmouseover event it's effect was implemented as an HTC and involves just a few lines of code. The code consists mainly of script, with a number of custom elements used to define the behavior. Notice that the use of the PUBLIC:ATTACH element that allows an HTC to listen in on events fired on the element on the page and to handle the events appropriately. So it provides the means to encapsulate the event handling code that would otherwise be put on the page.