Asynchronous JavaScript and XML (Ajax)

Web Architecture [./]
Fall 2008 — INFO 290-03 (CCN 42584)

Erik Wilde, UC Berkeley School of Information
2008-10-16

Creative Commons License [http://creativecommons.org/licenses/by/3.0/]

This work is licensed under a CC
Attribution 3.0 Unported License
[http://creativecommons.org/licenses/by/3.0/]

Contents E. Wilde: Asynchronous JavaScript and XML (Ajax)

Contents

E. Wilde: Asynchronous JavaScript and XML (Ajax)

(2) Abstract

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.



Dynamic HTML (DHTML)

Outline (Dynamic HTML (DHTML))

  1. Dynamic HTML (DHTML) [13]
    1. Document Object Model (DOM) [7]
    2. JavaScript [5]
  2. Ajax Basics [2]
  3. JavaScript Object Notation (JSON) [3]
  4. Conclusions [1]
Dynamic HTML (DHTML) E. Wilde: Asynchronous JavaScript and XML (Ajax)

(4) Scripting on the Web



Document Object Model (DOM)

Outline (Document Object Model (DOM))

  1. Dynamic HTML (DHTML) [13]
    1. Document Object Model (DOM) [7]
    2. JavaScript [5]
  2. Ajax Basics [2]
  3. JavaScript Object Notation (JSON) [3]
  4. Conclusions [1]
Document Object Model (DOM) E. Wilde: Asynchronous JavaScript and XML (Ajax)

(6) Handling Markup

  • HTML (and XML) are based on a tree model
    • there is one document element: html
    • elements may contain elements: head or body
    • element structures can be nested as deep as required
    • elements may contain text
    • elements may contain text mixed with elements
    • elements may have attributes
  • Most Web pages are not syntactically correct
    • basic markup errors: <b><i>no tree<b><i>
    • well-formed markup but not valid HTML
  • Browsers do their best to interpret everything as HTML
    • fixing errors always is based on assumptions and thus risky


Document Object Model (DOM) E. Wilde: Asynchronous JavaScript and XML (Ajax)

(7) Mixed Content

The term Mixed content in XML refers to elements which have text content mixed with elements [http://www.w3.org/TR/xml/#sec-mixed-content]. What these elements do depends on the elements smiley.gif, but the important point is that they are on the same level as the text nodes of the mixed content.

<p>The term <em>Mixed content</em> in XML refers to elements <a href="http://www.w3.org/TR/xml/#sec-mixed-content">which have text content mixed with elements</a>. What these elements do depends on the elements <img style="height : 1em" src="smiley.gif"/>, but the important point is that they are on the same level as the text nodes of the mixed content.</p>
XML tree for mixed content

Document Object Model (DOM) E. Wilde: Asynchronous JavaScript and XML (Ajax)

(8) 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, …


Document Object Model (DOM) E. Wilde: Asynchronous JavaScript and XML (Ajax)

(9) 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


Document Object Model (DOM) E. Wilde: Asynchronous JavaScript and XML (Ajax)

(10) HTML Parser

HTML Parser

Document Object Model (DOM) E. Wilde: Asynchronous JavaScript and XML (Ajax)

(11) DOM2 Map

dom2-map.png

Document Object Model (DOM) E. Wilde: Asynchronous JavaScript and XML (Ajax)

(12) DOM3 Map

dom3-map.png

JavaScript

JavaScript E. Wilde: Asynchronous JavaScript and XML (Ajax)

(14) Language Names

V8 JavaScript Engine
  • Netscape started the language as LiveScript
  • Netscape renamed the language to JavaScript
  • Microsoft implemented the language as JScript
  • ECMA [http://www.ecma-international.org/] standardized the language as ECMAScript
  • Macromedia/Adobe extended the language as ActionScript
  • Implementations for other platforms [http://en.wikipedia.org/wiki/List_of_ECMAScript_engines] are also available


JavaScript E. Wilde: Asynchronous JavaScript and XML (Ajax)

(15) Scripted Effects

<html>
 <head>
  <title>CSS and DOM for Text Animation</title>
  <script>
  cc = 25 ;  // Number of frames in the animation
  aa = 255 ; // Initial value used for RGB color
  tt = 0 ;   // Aux counter - the left position of the element
  el = 0 ;   // Index of the element
  function colorfade() {
   if ( cc > 0 ) {
    tt += cc ;
    document.getElementById("t"+el).style.left = tt+"px" ;
    aa -= 10 ;
    document.getElementById("t"+el).style.color = "rgb("+aa+","+aa+","+aa+")" ;
    cc-- ;
    setTimeout("colorfade()", 40) ;
   } else {
    cc = 25 ; aa = 255 ; tt = 0 ;
    if ( el < 5 ) {
     el++ ;
     setTimeout("colorfade()", 20); } } }
  </script>
 </head>
 <body onload="colorfade()" style="color:white;background:white;left:0px;">
  <div id="t0" style="position:absolute;top:10%;font-size:48pt;">CSS</div>
  <div id="t1" style="position:absolute;top:20%;font-size:32pt;">and</div>
  <div id="t2" style="position:absolute;top:30%;font-size:48pt;">DOM</div>
  <div id="t3" style="position:absolute;top:40%;font-size:32pt;">for</div>
  <div id="t4" style="position:absolute;top:50%;font-size:48pt;">Text</div>
  <div id="t5" style="position:absolute;top:60%;font-size:96pt;">Animation</div>
 </body>
</html>


JavaScript E. Wilde: Asynchronous JavaScript and XML (Ajax)

(16) Web Programming

  • Web programming always requires document access
    • user interactions are always going through the document (the interface)
    • user interactions create events
    • scripting code handles events and updates the document
  • Events occur in various contexts
    • Netscape decided that events bubble up the tree
    • Microsoft decided that events are captured top-down
    • the W3C DOM now has a combined capturing/bubbling model
    • event listeners have to decide when they want to be called


JavaScript E. Wilde: Asynchronous JavaScript and XML (Ajax)

(17) DOM Event Flow

dom-event-flow.png

JavaScript E. Wilde: Asynchronous JavaScript and XML (Ajax)

(18) Registering Event Listeners

<html>
 <head>
  <title>DOM Event Flow</title>
  <script type="text/javascript">
   function init() {
    // using old syntax to assign bubble-type event handlers
    window.onclick = winEvent ;
    document.onclick = docEvent ;
    document.body.onclick = docBodEvent ;
    // turn on click event capture for two objects
    document.addEventListener("click", docEvent, true) ;
    document.forms[0].addEventListener("click", formCaptureEvent, true) ;
    // set event listener for bubble
    document.forms[0].addEventListener("click", formBubbleEvent, false) ; }
   function winEvent(evt) { alert("window and (" + phase(evt) + ").") } ;
   function docEvent(evt) { alert("document and (" + phase(evt) + ").") } ;
   function docBodEvent(evt) { alert("body and (" + phase(evt) + ").") } ;
   function formCaptureEvent(evt) { alert("form and (" + phase(evt) + ").") } ;
   function formBubbleEvent(evt) { alert("form and (" + phase(evt) + ").") } ;
   function phase(evt) {
    switch (evt.eventPhase) {
     case 1:  return "CAPTURING" ; break ;
     case 2:  return "AT TARGET" ; break ;
     case 3:  return "BUBBLING" ;  break ;
     default: return "" } }
  </script>
 </head>
 <body onLoad="init()">
  <form>
   <input type="button" value="Click!" onClick="alert('button and (' + phase(event) + ').')" />
  </form>
 </body>
</html>


Ajax Basics

Outline (Ajax Basics)

  1. Dynamic HTML (DHTML) [13]
    1. Document Object Model (DOM) [7]
    2. JavaScript [5]
  2. Ajax Basics [2]
  3. JavaScript Object Notation (JSON) [3]
  4. Conclusions [1]
Ajax Basics E. Wilde: Asynchronous JavaScript and XML (Ajax)

(20) Ajax = DHTML + HTTP



Ajax Basics E. Wilde: Asynchronous JavaScript and XML (Ajax)

(21) Ajax and DHTML

Comparison of Ajax and DHTML

JavaScript Object Notation (JSON)

Outline (JavaScript Object Notation (JSON))

  1. Dynamic HTML (DHTML) [13]
    1. Document Object Model (DOM) [7]
    2. JavaScript [5]
  2. Ajax Basics [2]
  3. JavaScript Object Notation (JSON) [3]
  4. Conclusions [1]
JavaScript Object Notation (JSON) E. Wilde: Asynchronous JavaScript and XML (Ajax)

(23) JavaScript and XML



JavaScript Object Notation (JSON) E. Wilde: Asynchronous JavaScript and XML (Ajax)

(24) 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()" }
  ]
 }
}}


JavaScript Object Notation (JSON) E. Wilde: Asynchronous JavaScript and XML (Ajax)

(25) JSON via Content Negotiation



Conclusions

Outline (Conclusions)

  1. Dynamic HTML (DHTML) [13]
    1. Document Object Model (DOM) [7]
    2. JavaScript [5]
  2. Ajax Basics [2]
  3. JavaScript Object Notation (JSON) [3]
  4. Conclusions [1]
Conclusions E. Wilde: Asynchronous JavaScript and XML (Ajax)

(27) Client/Server Recreated



2008-10-16 Web Architecture [./]
Fall 2008 — INFO 290-03 (CCN 42584)