11.12.2012 Views

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

JavaScript 2.0-The Complete Reference, Second ... - freecodingtutorial

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Table 21-12: Some Default Behaviors Available in Internet Explorer 5+<br />

Behavior Description<br />

>saveFavorite Enables the current state of the page to be saved when the page is<br />

added to the user's list of ―Favorites.‖ Most often attached to a form<br />

and very useful for a ―login‖ page.<br />

>saveHistory Enables the current state of the page to be saved for the current<br />

browsing session. Whenever the user navigates back to the page,<br />

the page will be displayed with the saved state. Most often attached<br />

to a form.<br />

>saveSnapshot Enables the current state of the page to be saved when the user<br />

saves the page to the local file system.<br />

>userData Permits saving and retrieving large amounts of state information,<br />

even across multiple browsing sessions.<br />

As you can see, the default Behaviors are more related to browser functionality and state<br />

information than to traditional DHTML. <strong>The</strong> interfaces they expose can be a bit complex, so we<br />

will not get into the specifics of each (although an example of saving state information is<br />

included at the end of Chapter 16, and browser capabilities were touched upon in Chapter 17).<br />

Default Behaviors are attached to elements like any other Behavior, but the URL employed has<br />

the form<br />

#default#behaviorName<br />

where behaviorName is the name of the default Behavior you wish to attach. For example, to<br />

attach the userData Behavior to all form elements, you might use the following in your CSS<br />

definitions:<br />

form { behavior:url(#default#userData) }<br />

Full documentation of default Behaviors can be found at MSDN.<br />

Element Behaviors<br />

Whereas attached Behaviors augment or override the normal behavior of an existing element,<br />

element Behaviors are used to define new, customized elements. For instance, you can create<br />

your own rollover element, define default Behaviors for it, and include it in your pages as if it<br />

were a real part of HTML. You can even use attached Behaviors with new elements created in<br />

this fashion.<br />

Creating a custom element is like creating an attached Behavior. An HTC file is created using<br />

almost exactly the same syntax as you would use to define an attached Behavior. However, an<br />

element Behavior is imported into the page using XML, and after it has been imported, the new<br />

element can be used directly in the page without explicit binding to the HTC file. Because the<br />

HTC file defines the new element, there is no need to use addBehavior() or the CSS syntax<br />

used for attached Behaviors; doing so would be redundant. In fact, it is not possible to bind an<br />

element Behavior to an element as you would an attached Behavior.<br />

Element Behaviors are tremendously powerful, and well beyond the scope of this book.<br />

However, readers with an understanding of XML and the attached Behavior features discussed<br />

previously should have little problem creating their own elements. Aside from the highly<br />

nonstandard nature of element Behaviors, the only drawback of their use is that they are<br />

supported only by Internet Explorer 5.5+. For the time being, it might be advisable to stick with<br />

attached Behaviors until browser demographics shift heavily to newer versions of IE.<br />

Behaviors Versus Traditional DHTML

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!