Modularize your Web design with DHTML behaviors
Wednesday, July 23, 2003 12:00 PM
DHTML behaviors are the result of an evolutionary process of Web design under Internet Explorer. Previously, if you wanted to add functionality to IE, you had to use ActiveX controls, which raised a number of troubling security and architecture issues. Microsoft developed the HTML Components standard (still awaiting World Wide Web Consortium adoption) as an XML replacement for ActiveX controls.
Think
of HTML Components as encapsulated DHTML—you can implement them as reusable,
self-contained files. HTML Components are usually coded using VBScript or
JScript. More advanced behavior-driven applications can be scripted using
Windows Script Components (WSC) or any ActiveX-enabled programming
language.
The importance of HTML
Components
The obvious advantage of using HTML Components is that your
code becomes clean and modular. As with JavaScript source files (.js), you can
access and reuse your HTML Components code across multiple Web pages. Under HTML
Components, you can alter a component by modifying a single file rather than
making in-line changes throughout your Web site. Components encourage the
separation of content from functionality.
DHTML behaviors provide
numerous advanced programming tools, including extended browser methods,
properties, and scripting access across domains. Behavior functionality can be
attached and detached dynamically from HTML elements using script. Moreover,
Microsoft has made HTML Components an integral part of its .NET Web services
strategy.
Limitations of HTML
Components
The obvious limitation of this technology is the fact that
it's still proprietary, despite Microsoft's attempts to make it a standardized
format. HTML Components are supported in IE 5.0 and above. Netscape doesn't
support DHTML behaviors. Most browsers will ignore the CSS behavior attributes,
but references to behavior events, methods, or properties will definitely cause
scripting errors in non-IE software.
To successfully deploy DHTML
behaviors on a Web site, a browser detection routine is required to ensure the
user is running a compliant version of IE before launching the components. DHTML
behaviors are thus best suited for heterogeneous computing environments such as
intranets.
HTML Components rely on the Windows Scripting Host (WSH)
because they are coded using JScript and VBScript. If the user has disabled WSH
for security reasons, the components will not function. Moreover, HTML
Components are subject to all security limitations internally imposed by
IE.
DHTML behaviors won't work if your Web server doesn't parse all HTML
elements on a Web page. You should use readyState variables such as
oncontentready and ondocumentready to check for the availability
of the component before trying to launch a behavior. Otherwise, your users may
receive ugly scripting errors. More information on the readyState
variable is available in this article on MSDN.
How HTML
Components work
You can implement DHTML behaviors by creating a style
sheet (i.e., .css) linked to an HTML Component (i.e., .htc) for a specific DHTML
effect. If you're accessing behaviors prebuilt into the IE browser, you don’t
need an external .htc file. You simply need to add a couple of XML declarations
in your code and define the behavior from within your style
declaration.
Here's an example of the generic structure of an HTML
Component file:
<PUBLIC:COMPONENT>
<SCRIPT>
</SCRIPT>
</PUBLIC:COMPONENT>
Within
the HTC syntax and framework, you can:
- Make available behavior-specific properties, methods, and events.
- Use any element from the DHTML Object Model.
- Receive DHTML and HTC event notifications.
To get a better understanding of how they work, let's examine two types of behaviors you can deploy using IE: the attached behavior and the element behavior.



Does anyone know where I might find a script that will: Prompt a visitor to set a web site as a homepage, and if the user does have that homepage set, then they will not see the prompt again? Any help would be greatly appreciated.
Posted by Matt Brodersen on Tuesday, May 18 2004 04:32 AM