|
||
| Inside Technique : HTML Text Editor : Creating Behaviors DHTML Behaviors are created using XML. While HTML is defined by a fixed set of tags and semantics,
XML allows custom markup to be defined for packaging meta-information and data.
Below is the XML for defining a minimal DHTML behavior. Notice the special tags for specifying the scriptlet's definition:
The scriptlet tag specifies we are creating a scriptlet, and the implements tag defines this scriptlet to be a DHTML Behavior. The implements element represents the COM interface handler for the scriptlet, where behaviors are an interface recognized by Internet Explorer 5.0. Therefore, with scriptlets, you can actually create reusable COM components that are unrelated to the browser. Creating non-behavior COM components is beyond the article (see Microsoft Sitebuilder's for an article on scriptlet components). In addition, the implements element is also used to specify any custom methods, properties, and events exposed by the component. We will provide a more in-depth tutorial on creating behaviors and their interfaces in a future article. The functionality for the scriptlet is contained within the script block. The script can be written in JavaScript/ JScript, VBScript or any language you may have installed. Within the script block, you write the appropriate functions and event handlers for processing the behavior. Usually the first step is to define what events you want to receive from the browser. This is accomplished with the attachEvent() method. With this method you specify the event you want to catch, and a function to execute when it is loaded. The attachEvent() method is used over the traditional event property model where you assign a function directly to the property to allow broadcasting of the event. This is necessary so behaviors augment existing functionality and do not replace it. For example, the behavior's onclick event should not prevent the element itself, or possibly other behaviors, from receiving the same event. To attach the click event, we used the following line of script:
In the case of our behavior, we want to replace the existing HTML in the document with new HTML.
This is done by modifying the innerHTML of the element. The innerHTML and all properties of the element
associated with the behavior are directly accessible by the behaviors script. This is because the
element with the behavior is represents the current scope for all immediately executed code.
A behavior can therefore replace the contents of the element as follows:
Getting quickly into the details of our htmlArea behavior, we provide the complete source code below. When examining
the code, notice that we are replacing the contents of the element with the editor user-interface and HTMLArea.
Since we want to later program the HTMLArea, we give the HTMLArea a unique id. To generate the unique ID, we use a new method,
uniqueID, that automatically finds an unused ID for use by our behavior. With this ID, we are able to access and attach to the events of
the newly inserted HTMLArea element:
We hope this provides you with a very quick introduction to DHTML behaviors. We recommend you check out Microsoft's Sitebuilders for more information on behaviors. Also, keep checking back with us as we are planning to cover behaviors in more detail with more examples in the future (for example, we want to convert all our scriptlet examples to behaviors. any volunteers to help?). Editor ImprovementsWe wrote the editor to show off the HTMLArea element and DHTML Behaviors. However, the editor is still not ready for primetime. To make the editor really useful and even more down-level friendly, we would add the following two improvements:
This concludes our first article on Internet Explorer 5.0 Developer's Preview. Be sure to visit our feedback discussion forum and let us know if you want us to continue with more Internet Explorer 5.0 coverage. Page 1:HTML Text Editor © 1997-2000 InsideDHTML.com, LLC. All rights reserved. |