CSS Behavior
Microsoft calls this method for adding behavior DHTML Behaviors. As far as I know it has not (yet) been submitted to the W3C for consideration, and I'm not sure if it ever will.
The principle of this mechanism is very simple. All you have to do is add the script to the XML element, as shown in Listing 21.13.
Listing 21.13 An HTML File with Dynamic Behavior
1: <html>
2: <head>
3: <title>Jumping XML</title>
4: <XML:namespace prefix="IE5"/>
5: <style>
6: @namespace IE5 {
7: getsclass {
8: font-family: courier; color: red; font-weight: bold;
9: border-style: solid; background-color: yellow;
10: border-color: black; border-width: 3; width: 240;
11: height: 140; padding: 10
12: }
13: }
14: </style>
15: </head>
16: <body>
17: <h1>Application of CSS Behavior to XML</h1>
18: <P>This paragraph is standard HTML code.</p>
19: <IE5:getsclass onclick="this.style.position = 'static';"
20: style="position: absolute;
21: top: 200; left: 200">This paragraph is XML code, with behavior
22: attached to it by a style.
23: Click on it, and it will jump
24: to a new position.</IE5:getsclass>
25: <P>This paragraph is also standard HTML code,
26: it will make room for the XML
27: paragraph when it moves.</p></body>
28: </html>
Figures 21.10 and 21.11 illustrate what the browser displays before and after the addition of the script to the XML element.
Figure 21.10 The dynamic XML paragraph in Internet Explorer 5 before it's clicked.
Figure 21.11 The dynamic XML paragraph in Internet Explorer 5 after it's been clicked.
| Embedding the behavior works, as you can see from the before and after illustrations (Figures 21.10 and 21.11). However, it only gets us part of the way there because the code is still inside the HTML/XML document. |
Microsoft's solution for importing externally defined behavior, like Netscape's solution, is to use the existing CSS style sheet mechanism. Microsoft, though, has also slightly extended the CSS syntax to add a new behavior property. I have rewritten Listing 21.13 to externalize this behavior and titled it Listing 21.14.
|
Listing 21.14 An HTML File with Dynamic Behavior
|
1: <html>
2: <head>
3: <title>Jumping XML</title>
4: <XML:namespace prefix="IE5"/>
5: <style>
6: IE5\:jump {style="font-color: red"; behavior: url(jump.sct);}
7: </style>
8: </head>
9: <body>
10: <h1>Application of CSS Behavior to XML</h1>
11: <P>This paragraph is standard HTML code.</p>
12: <IE5:jump>This paragraph is XML code, with behavior
13: attached to it by a style.
14: style. Click on it, and it will jump
15: to a new position.</IE5:getsclass>
16: <P>This paragraph is also standard HTML code,
17: it will make room for the XML
18: paragraph when it moves.</p></body>
19: </html>
| Listing 21.14 behaves exactly the same way as Listing 21.13 (I do, of course have to shift the behavior code into the file pointed to by the URL in the behavior property). Note that in Listing 21.14, I have used an embedded style sheet (using the STYLE tag), but I could just as easily have moved this to an external style sheet. |
Microsoft seems to have great plans for behaviors. One of the many things rumored to be on the way is the ability to not just use scripts to attach behavior to elements, but also ActiveX controls. In practice, this could have much the same effect as allowing a Web page to run a normal program on your computer. While it opens up exciting possibilities for extending the capabilities of Web pages (imagine logging into a help desk for a software package and having an interactive demonstration of how to use a feature on your own screen), the thought of allowing this kind of access does raise the usual questions concerning security. Whatever happens, it will most certainly be interesting.