HTML Client Side
HTML Client Side
Manipulating
Client-Side
HTML
Introduction The Roles and
Responsibilities of Client-Side Languages
Introduction
Both hybrid and single-page interface (SPI)
web applications that do not perform fullpage post backs in order to change the
rendered user interface (Up must use clientside techniques for manipulating the
document object model (DOM). This DOM
manipulation could include updating the
content of the page, displaying new UI
elements, or loading entire pages in response
to user gestures and other events. The three
client-side technologies you can use when
implementing these techniques are HTML,
cascading style sheets (CSS) and JavaScript.
This chapter discusses how you can clearly
define the roles of HTML, CSS, and
JavaScript on the client to ensure that
developers and designers can work together
without affecting each other's code. You will
also see how to use JavaScript to manipulate
HTML effectively in the browser.
In this chapter you will learn
Structure, Appearance,
and Behavior
There are three areas of responsibility within
client applications, and each client-side
language is associated with one of those
areas. The three areas are:
Adding Interactive
Behavior to an
Application
When you have a simple web application, you
can rely on the web server to handle changes
to the HTML by posting back to the server so
that it can update the page, or by loading a
different page that contains the required
HTML. However, this approach limits the level
of interactivity in the application.
There are several ways that JavaScript code
can add interactivity to a page through
manipulation of the HTML structure and
content without requiring a full-page reload.
Some typical approaches are:
Note:
Selecting and
Manipulating Existing
HTML Elements
Many scenarios for interactive web applications
only require client-side languages such as
CSS and JavaScript to modify existing
elements. Modification usually involves
adding, removing, or updating the attributes
and style classes applied to elements. In
some cases, CSS alone may be capable of
performing the required changes, such as
changing the style of hyperiink elements as
the mouse pointer passes over them. In other
cases, JavaScript code is required to
manipulate the elements directly.
Before you can perform actions on elements,
you must specify how the CSS or JavaScript
will select them. You can select one or more
elements, as described in the following
section, and then use a range of techniques to
manipulate the elements. These techniques
are described in "Modifying Existing
Elements," later in this chapter.
Selecting Elements
There are several ways you can use selectors
'1 Note:
All of these selection methods run the risk
of impacting client-side performance. It is
important to ensure that you provide
additional context with the selectors to
prevent searching the entire DOM. See
"Using Context" below for more
information.
Using Context
One of the ways to achieve maximum
performance when selecting elements is to
specify a context to search within. This
context allows searches based on a known
and referenced element. There are two ways
to provide this context. The Auenj method
can take a second parameter, a known
element, to scope the search through the
DOM. Alternatively, the find method of an
</div>
HTML
"IL Note:
Only the first hyphen in the attribute name
is significant. Everything in the attribute
name after the first hyphen is treated as a
sinnple string value. For example, datalist-url="Nehicle/JsonList" is a dataattribute containing the metadata listurl="Nehicle/JsonList".
Modifying Existing
Elements
After you select your target element or set of
elements, you can modify them as required.
The following sections provide some
examples of scenarios and solutions for
modifying existing elements within the HTML
structure of the page.
Note:
JavaScr ipt
// Contained in mstats.fillul
bindNavigation: function
() var that = this;
this.element.delegate('D
if ($(this).dataCacthat. setOption(
event.preventDefi
Generating HTML
Dynamically with
JavaScript and Auery
When developing a hybrid or SPI application,
you often need to modify the existing page by
inserting into it fragments of new HTML. You
can, of course, remove elements from the page
as well, but it is usually easier to change the
CSS styles that control visibility.
Mileage Stats generates and inserts
fragments of HTML into its pages in several
places. For example, it uses JavaScript to add
new elements to the page structure as the
user navigates through the application. The
following code (it is part of the info pane
widget) creates the HTML structure that
JavaScript
/1 Contained in rnstats.info-1
setupDetallsPane: function
I var elern = this.element,
options = this.optioi
if (!elem.find("#details.
elem.find("div:first
.append('<div id:
}
this.vehicleDetails = ell
1/ options for the w:
));
},
This code uses the find method to check if the
Details pane is already present in the
structure. If it does not exist, the code locates
the first div element within the current context
and uses the append method to insert a new
div element with id="details-pane" into the
page.
Generally, the amount of HTML generated with
script should be small. Script that creates
Retrieving HTML
Fragments from the
Server
In some situations, it may make sense to
retrieve an HTML fragment from another
source (for example, a URL on the web
server) and inject it directly into the page,
perhaps after manipulating it, rather than
dynamically generating the content on the
client. For example, it may make sense to
reuse an HTML form generated on the server
and dynamically embed it in a web page,
rather than generating the entire form and its
contents with JavaScript. This approach can
simplify the client-side code by moving some
of the complexity to the server.
Using Templates to
Retrieve and Generate
HTML
As mentioned in the previous section, another
option for generating HTML is to use HTML
// Contained in mstats.stati
applyTemplate: function (da'
var options = this.optioi
Stemplate = $(option:
I
this.element.find(141stat:
.html(StemplateAmpli
}
following HTML is generated:
For more information on the jQuery Template
HEAL
// Run-time HTML
<div id="statistics-content":
<h1>Summary Statistics </h1:
<div class="statistic mile.
class="units">mpvispan>.
</div>
plug-in and authoring templates, see "jQuery
CS HTML
<-- Contained in _TemplateVel
<script id="mstats-add-vehic:
<div class="framed command
<div>
<a data-action="vehicli
href="ftrl.Actionri
<Idly>
</div>
</script>
The _TemplateVehicleList template shown
above is processed by the Razor rendering
engine using the @Html.Partial view helper
method in _Layout.cshtmli as shown below.
CSHTML
<I-- Contained in Layout.cs1
@Html.Partial("_TemplateVehii
The HTML fragment shown below is the
rendered _TemplateVehicleList template
when the web application is deployed to the
root web site on the server.
HTML
<I-- Rendered to the browser
<script id="mstats-add-vehic.
<div class="framed command
<div>
<a data-action="vehicL
href="/vehicle/add":
</div>
</div>
</script>
If, instead, the web application is deployed to
the MileageStats folder in the root website, the
rendered HTML will be the code shown below.
You can see the folder name in the value of the
href attribute.
HTML
S u m m a r y
There are three key technologies that work
together in the browser to enable interactive
applications: HTML, CSS, and JavaScript.
HTML specifies the structure of the elements
available within the L11, 055 controls the
styling and positioning of those elements, and
JavaScript allows the element s to respond to
user gestures and other events such as timers
and animations.
Within an application, there is often a
requirement to manipulate the HTML that
generates the page the user sees, and there
are a number of options for selecting the
HTML elements whose appearance or behavior
you wish to change. Of these, the use of
relative position provides the best
performance, while using data- attributes can
significantly help with both the selection of
HTML elements and the transmission of
metadata to the client code. Data- attributes
also help to ensure separation between the
HTML and the metadata the application
requires.
Further Reading
jQuery Home Page: http://jquery.com
jQuery Browser Compatibility
http://docs.jduery.com/Browser_Compatibility
jQuery Selectors:
http://api.jquery.com/categoryiselectors
jQuery Templates:
http://api.jduery.comicategoryipluginsitempl
aces
For more information on how the server can
provide the correct initial HTML to the client,
see "Providing HTML Structure" in Chapter 11,
"Server-Side Implementation."
For more detail on the BBQ plug-in and the use
of a state object, see Chapter 9, 'Navigation."