(2) Abstract
Scripting is used on the majority of today's modern Web sites. Scripting can be used to improve the usability and accessibility of a Web site (for example for validating form data on the client side), it can vastly improve the user experience with new interface design (the smooth scrolling of Google Maps vs. older click to scroll
map services), or it can be used to implement behavior that would be impossible without scripting (for example the online applications of Google Docs). Asynchronous JavaScript and XML (Ajax) takes Dynamic HTML (DHTML) to the next level by allowing server access from within scripting code. This is accomplished by using a standardized API for client/server communications, the XMLHttpRequest
object. This objects allows using HTTP connections from within scripting code, and thereby allows scripting code to dynamically reload data from a server in response to user interactions.
(5) Basic Scripting (DHTML)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Well-Designed JavaScript</title>
<script type="text/javascript" src="nicetitle.js"></script>
<link rel="stylesheet" href="nicetitle.css" />
</head>
<body>
<h1>Well-Designed JavaScript</h1>
<p><a href="http://en.wikipedia.org/wiki/JavaScript" title="Wikipedia: JavaScript is a scripting language used to enable programmatic access to objects within other applications. It is primarily used in the form of client-side JavaScript for the development of dynamic websites.">JavaScript</a> should not make any assumptions about browser support. Ideally, pages using scripting should also be usable when scripting is turned off, so that more basic browsers (for example, mobile phones or Kindles) can also be used for using the page.</p>
</body>
</html>
(6) Basic Scripting (JavaScript)
if( !document.links )
{
document.links = document.getElementsByTagName("a");
}
for (var ti=0;ti<document.links.length;ti++) {
var lnk = document.links[ti];
if ( lnk.title ) {
lnk.setAttribute("nicetitle",lnk.title);
lnk.removeAttribute("title");
addEvent(lnk,"mouseover",showNiceTitle);
addEvent(lnk,"mouseout",hideNiceTitle);
addEvent(lnk,"focus",showNiceTitle);
addEvent(lnk,"blur",hideNiceTitle);
}
}
var d = document.createElementNS(XHTMLNS,"div");
d.className = "nicetitle";
tnt = document.createTextNode(nicetitle);
pat = document.createElementNS(XHTMLNS,"p");
pat.className = "titletext";
pat.appendChild(tnt);
d.appendChild(pat);
Document Object Model (DOM)
(13) From HTML to DOM
- HTML [Hypertext Markup Language (HTML)] is a representation for hypermedia documents
- a representation is required to store and transmit the document
- HTML uses markup [Hypertext Markup Language (HTML); Structured Documents on the Web (1)] for representing the document structure
- Browsers must render HTML documents (i.e., apply CSS and execute JavaScript)
- GET HTML from server and receive as text/html document
- parse document and deal with any errors by
fixing them
- interpret document as if it had been error-free
- GET all additional resources (CSS, images, JavaScript, …)
- build internal model (DOM) based on error-free interpretation
- apply CSS rules to determine styling of document (e.g., margins and font sizes)
- render into visual structure
- start executing JavaScript code
- listen for events (keyboard, mouse, timer) and execute code
- discard everything and start over when user navigates to a different page
(14) Browser Handling of HTML

(15) Document
- The document (HTML) is the interface language for Web applications
- Most programming environments have visual interface models
- almost everything has moved to window-oriented interfaces
- Windows, MacOS, and Linux provide similar visual metaphors
- Web applications must use HTML as their model for the interface
- HTML Forms [HTML Forms] are a simple way to build an interface
- forms can be extended with client-side helpers (validation, repeating entries)
(16) Object
- Documents are static, programming is dynamic
- documents and code must be connected
- objects are a common abstraction in programming languages
- Objects usually have a type and methods
- types for HTML-based objects are based on HTML's elements
- methods define the allowed to interact with objects
- interactions can be read-only or they can change the document structure
for (var ti=0;ti<document.links.length;ti++) {
var lnk = document.links[ti];
if ( lnk.title ) {
lnk.setAttribute("nicetitle",lnk.title);
lnk.removeAttribute("title");
addEvent(lnk,"mouseover",showNiceTitle);
addEvent(lnk,"mouseout",hideNiceTitle);
addEvent(lnk,"focus",showNiceTitle);
addEvent(lnk,"blur",hideNiceTitle);
}
}
(17) Model
- Models are idealized/abstract views of something
- Model interfaces allow to expose that idealized/abstract view
- DOM introduced a common way of how browsers deal with HTML
- without a DOM, there can be no interoperable scripting
- Abstractions are also limitations
- some vendors add/support
extensions
to the basic DOM model - any code based on these extensions is not interoperable
(18) Markup to Trees
- Browsers build a DOM tree from the (fixed) markup
- scripting code never works on the markup text, it works on the tree
- DOM trees are an abstraction of the markup
- trees provide convenient navigation facilities
- abstractions provide an insulation against irrelevant markup details
- DOM is available in a large number of programming languages
- JavaScript [JavaScript (1)] is the language supported in Web browsers
- DOM is also available for Java, C, C++, Python, Perl, C#, Ruby, …
(19) DOM History
- DOM0 was invented by Netscape (backing the LiveScript/JavaScript)
- DOM1 was the first DOM version produced by the W3C
- DOM2 is the currently available stable version of DOM
- major changes to be compliant with XML and the XML Infoset
- DOM3 is highly modularized and still under development
- more XML technologies such as the ability to use XPath
(20) DOM2 Map

(21) DOM3 Map

Asynchronous JavaScript and XML (Ajax)
(23) Ajax = DHTML + HTTP
- Basic Scripting (DHTML) [Basic Scripting (DHTML) (1)] uses JavaScript
locally
- the scripting code reacts to user events and accesses the DOM structure
- changes are either hardcoded or derived from user events
- Ajax [Asynchronous JavaScript and XML (Ajax) (1)] adds an HTTP request [Web Foundations (URI & HTTP); HTTP Requests (1)] method to JavaScript
- scripting code can now request additional data from an HTTP server
- changes can thus be made based on any data received from the server
- Ajax dramatically reduces the number of page reloads
- any change of the page can be done without a complete reload
- based on user interactions, parts of the page can be reloaded
- Ajax has the same interoperability problems as DHTML
(24) Ajax and DHTML

(25) JavaScript and XML
- The
XMLHttpRequest
API has been built for requesting XML via HTTP- this is useful because XML is the most popular data format
- all requested data has to be processed by using XML access methods in JavaScript
- JavaScript does not have XML as its internal data model
- the XML received via
XMLHttpRequest
has to be parsed into a DOM tree - DOM access in JavaScript is inconvenient for complex operations
- alternatively, the XML can be mapped to JavaScript objects (also requires parsing)
- JavaScript Object Notation (JSON) encodes data as JavaScript objects
- more efficient for the consumer if the consumer is written in JavaScript
- this turns the generally usable XML service into a JavaScript-oriented service
- for large-scale applications, it might make sense to provide XML and JSON
- using HTTP Content Negotiation [Web Foundations (URI & HTTP); HTTP Content Negotiation (1)], this can be negotiated on the HTTP protocol level
(26) JSON Example
<?xml version="1.0"?>
<menu id="file" value="File">
<popup>
<menuitem value="New" onclick="CreateNewDoc()"/>
<menuitem value="Open" onclick="OpenDoc()"/>
<menuitem value="Close" onclick="CloseDoc()"/>
</popup>
</menu>
{ "menu" : {
"id" : "file",
"value" : "File",
"popup" : {
"menuitem" : [
{ "value" : "New", "onclick" : "CreateNewDoc()" },
{ "value" : "Open", "onclick" : "OpenDoc()" },
{ "value" : "Close", "onclick" : "CloseDoc()" }
]
}
}}
(27) JSON via Content Negotiation
- XML or JSON are just different representations
- they represent the same underlying resource (as identified by the URI)
- HTTP Content Negotiation [Web Foundations (URI & HTTP); HTTP Content Negotiation (1)] allows to specify preferences
- clients specify preferences and server respond with the best match
- HTTP Accept specifies the accepted Media Types [Media Types]
- resources may be available in HTML, XML, or JSON
- depending on the HTTP request, the server responds with the best representation
- reduces processing time on the client and can be cached
- Really smart Ajax frameworks could even hide the content negotiation
- request JSON or XML, with XML being the lower priority
- if XML is sent by the server, it has to be parsed into a JavaScript object
- the end result is always a JavaScript object for the framework user