DHTML Behaviors

April 2, 2005

DHTML Behaviors are useful features utilized by Internet Explorer. They’re often overlooked because other browsers don’t support them. Usage, therefore, is generally limited to intranet applications where the users’ configuration is homogenous and controlled.

DHTML Behaviors are used for associating code with an HTML tag through a CSS file. For instance, putting the following style into either a CSS file or directly into a <style> tag will associate the code inside the .htc file with the anchor tag.

A {
BEHAVIOR: url(../css/LinkBehave.htc)
}

The code inside the LinkBehave.htc file:

< PUBLIC:COMPONENT NAME=”LinkBehave” lightWeight=”true” >
<ATTACH EVENT=”onclick” HANDLER=”DoClick” />
</PUBLIC:COMPONENT>

<SCRIPT LANGUAGE=”javascript”>
<!–
function DoClick() {
     if (window.event != null) {
          if (window.event.shiftKey != 0 ) {
              window.event.cancelBubble = true;
              window.event.returnValue = false; 
              return false; 
          } 
    } 
    return true;
} //–>
</SCRIPT>

Once loaded, the DoClick function is called every time the user clicks on an anchor tag, any anchor tag. When DoClick() is called, it checks to ensure the shift key is not pressed. If it is, it ignores the click, preventing IE from opening the link inside a new window. This is handy when you’re developing an intranet application that needs to prevent the user from performing certain actions.

Here is a link to an extremely useful example of a DHTML behavior, this one allows you to define a or tag that is dynamically populated through a web service. Now that is really handy. Imagine dynamically populating part of a page, based on the selection of a control. All without the need to reload the entire page again.

Facebooktwittermail