10.02.2014 Views

Beginning Ajax With ASP.NET (2006).pdf

Create successful ePaper yourself

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

Chapter 9<br />

Like many other <strong>Ajax</strong>-style libraries and frameworks, this library provides a layer of abstraction around<br />

the XMLHttpRequest object and shields the developer from having to know any intricate details about<br />

coding against that specific object. The purpose of this library is somewhat different and more focused<br />

than some other libraries available. Most other frameworks and libraries provide generic mechanisms<br />

around construction and manipulation of the XMLHttpRequest object and manipulation of various<br />

aspects or functionality surrounding that object, such as “supporting” technologies like X Path, XSLT,<br />

and the like. This framework is specifically geared at the asynchronous loading and displaying of HTML<br />

content, hence, the name HTMLHttpRequest. HTML content is the focus, and various mechanisms make<br />

the loading and displaying of that content very easy, and make it occur in an asynchronous manner.<br />

One of the major differentiators that this library offers is the unique way of invoking an asynchronous<br />

request. A direct method call is not required (although this can be used) to load a remote document.<br />

Instead, decorating tags in the document with a specific class style reference cause<br />

an asynchronous load of the referenced document to occur and place it in a predefined location, dynamically<br />

within the HTML document. An example demonstrates this best:<br />

/*<br />

HTMLHttpRequest v1.0 beta2<br />

(c) 2001-2005 Angus Turnbull, TwinHelix Designs http://www.twinhelix.com<br />

Licensed under the CC-GNU LGPL, version 2.1 or later:<br />

http://creativecommons.org/licenses/LGPL/2.1/<br />

This is distributed WITHOUT ANY WARRANTY; without even the implied<br />

warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.<br />

*/<br />

My HTML<br />

Content Document<br />

<br />

Area to have document loaded into<br />

<br />

The preceding code example asynchronously loads in the SomePage.html HTML file from the<br />

SomeDirectory directory and dynamically places that content inside the element, which has an<br />

id of displayDocument. Notice that this code contains no JavaScript at all, yet the asynchronous loading<br />

behavior of the XMLHttpRequest object is being utilized. The magic is in the naming system of the<br />

class attribute of the element and the element.<br />

An element with a class attribute that contains the loadinto-{control_id} text, indicates that when<br />

this element receives a mouse click, the underlying framework should create an XMLHttpRequest object<br />

and use it to perform an asynchronous request for the document specified in the href attribute. Once the<br />

document is received, the framework then locates the control that has an id attribute that matches the<br />

second part of the loadinto-{control_id} class attribute (that is, the {control_id} portion of that<br />

attribute). In the preceding example, the control ID to search for is displayDocument. The document<br />

that has been loaded is then assigned to the .innerHTML property of the control.<br />

Additional functionality includes creating a “blind” toggle where a link is clicked and toggles the display<br />

of a content section, which is often used on sites that display articles and contain a Read More link<br />

to expand the entire article. This also utilizes a similar method to that described previously with specific<br />

class attribute definition to achieve this dynamic behavior.<br />

200

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

Saved successfully!

Ooh no, something went wrong!