JavaScript, AJAX, jQuery, Code, OH MY! Or: How cool web stuff works
The Web That Was Web Page Web Page CGI
No reloading OMG LMAO!
 
<! DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot; > < html xmlns = &quot;http://www.w3.org/1999/xhtml&quot; > < head > < meta http-equiv = &quot;Content-Type&quot; content = &quot;text/html; charset=ISO-8859-1&quot; /> < title >I'm a web page</ title > </ head > < body > < div id = &quot;top&quot; >This is the top.</ div > </ body > </ html >
The Document Object Model (DOM) XML Document Object Model
JavaScript! Magic?
Referencing an XHTML element < div id = &quot;top&quot; >This is the top.</ div > … reference the element by unique ID document. getElementById( &quot;top&quot; ) … this returns the selected element in the parse tree. DOM Element Properties
How to Lose Friends <! DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot; > < html xmlns = &quot;http://www.w3.org/1999/xhtml&quot; > < head > < meta http-equiv = &quot;Content-Type&quot; content = &quot;text/html; charset=ISO-8859-1&quot; /> < title >I'm a web page</ title > < script type= &quot;text/javascript&quot;> function element() { alert( document. getElementById( &quot;top&quot;).innerHTML); } </ script> </ head > < body onload = &quot;element()&quot; > < div id = &quot;top&quot; >This is the top.</ div > </ body > </ html >
Manipulating Element Contents InnerHTML! DOM Manipulation!
Events <! DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot; > < html xmlns = &quot;http://www.w3.org/1999/xhtml&quot; > < head > < meta http-equiv = &quot;Content-Type&quot; content = &quot;text/html; charset=ISO-8859-1&quot; /> < title >I'm also a web page</ title > < script type= &quot;text/javascript&quot;> function update() { document. getElementById( &quot;contents&quot;).innerHTML = &quot;You clicked!&quot;; } </ script> </ head > < body > < div id = &quot;contents&quot; >I'm here when you initially load.</ div > < input type = &quot;button&quot; value = &quot;Click Me&quot; onclick = &quot;update()&quot; /> </ body > </ html > Try Me
Complicated Behavior… Path101
XMLHttpRequest() XMLHttpRequest (XHR) transfers data between a client (browser) and a server over background HTTP requests.
Using XMLHttpRequest …in a cross-browser friendly way var request = new XMLHttpRequest(); request. open( &quot;GET&quot;, url, false); request. send( null); if(!request. getResponseHeader( &quot;Date&quot;)) { var cached = request; request = new XMLHttpRequest(); var ifModifiedSince = cached. getResponseHeader( &quot;Last-Modified&quot;); ifModifiedSince = (ifModifiedSince) ? ifModifiedSince : new Date( 0); // January 1, 1970 request. open( &quot;GET&quot;, url, false); request. setRequestHeader( &quot;If-Modified-Since&quot;, ifModifiedSince); request. send( &quot;&quot;); if(request. status == 304) { request = cached; } }
JavaScript Libraries A good library will abstract away the ugly details of cross-platform code. Prototype (the first major lib) jQuery (write less, do more) Script.aculo.us (the prettiest) YUL! (the yahoo!iest) Etc…
jQuery < html > < head > < script type= &quot;text/javascript&quot; src= &quot;jquery.js&quot;></ script> <script type= &quot;text/javascript&quot;> // Your code goes here </ script> </ head > < body > < a href = &quot;http://jquery.com/&quot; >jQuery</ a > </ body > </ html >
jQuery Selector Syntax $(‘#elementid’) $(‘.classname’) $(‘#content a.link’)
Events $(‘#top’).click(); $(‘#top’).click(function() { // do stuff when #top is clicked });
Document Ready Not “onload”! $(document).ready(function(){ // Your code here });
Local Example < html > < head > < script type= &quot;text/javascript&quot; src= &quot;http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js&quot;></ script> <script type= &quot;text/javascript&quot;> $( document).ready( function() { $( &quot;#content&quot;). click( function() { alert( &quot;hi&quot;); }); }); </ script> </ head > < body > < div id = &quot;content&quot; >Did anyone go to Denny's today?</ div > </ body > </ html >
… explore… Thank you!

JWU Guest Talk: JavaScript and AJAX

  • 1.
    JavaScript, AJAX, jQuery,Code, OH MY! Or: How cool web stuff works
  • 2.
    The Web ThatWas Web Page Web Page CGI
  • 3.
    No reloading OMG LMAO!
  • 4.
  • 5.
    <! DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot; > < html xmlns = &quot;http://www.w3.org/1999/xhtml&quot; > < head > < meta http-equiv = &quot;Content-Type&quot; content = &quot;text/html; charset=ISO-8859-1&quot; /> < title >I'm a web page</ title > </ head > < body > < div id = &quot;top&quot; >This is the top.</ div > </ body > </ html >
  • 6.
    The Document ObjectModel (DOM) XML Document Object Model
  • 7.
  • 8.
    Referencing an XHTMLelement < div id = &quot;top&quot; >This is the top.</ div > … reference the element by unique ID document. getElementById( &quot;top&quot; ) … this returns the selected element in the parse tree. DOM Element Properties
  • 9.
    How to LoseFriends <! DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot; > < html xmlns = &quot;http://www.w3.org/1999/xhtml&quot; > < head > < meta http-equiv = &quot;Content-Type&quot; content = &quot;text/html; charset=ISO-8859-1&quot; /> < title >I'm a web page</ title > < script type= &quot;text/javascript&quot;> function element() { alert( document. getElementById( &quot;top&quot;).innerHTML); } </ script> </ head > < body onload = &quot;element()&quot; > < div id = &quot;top&quot; >This is the top.</ div > </ body > </ html >
  • 10.
    Manipulating Element ContentsInnerHTML! DOM Manipulation!
  • 11.
    Events <! DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot; > < html xmlns = &quot;http://www.w3.org/1999/xhtml&quot; > < head > < meta http-equiv = &quot;Content-Type&quot; content = &quot;text/html; charset=ISO-8859-1&quot; /> < title >I'm also a web page</ title > < script type= &quot;text/javascript&quot;> function update() { document. getElementById( &quot;contents&quot;).innerHTML = &quot;You clicked!&quot;; } </ script> </ head > < body > < div id = &quot;contents&quot; >I'm here when you initially load.</ div > < input type = &quot;button&quot; value = &quot;Click Me&quot; onclick = &quot;update()&quot; /> </ body > </ html > Try Me
  • 12.
  • 13.
    XMLHttpRequest() XMLHttpRequest (XHR)transfers data between a client (browser) and a server over background HTTP requests.
  • 14.
    Using XMLHttpRequest …ina cross-browser friendly way var request = new XMLHttpRequest(); request. open( &quot;GET&quot;, url, false); request. send( null); if(!request. getResponseHeader( &quot;Date&quot;)) { var cached = request; request = new XMLHttpRequest(); var ifModifiedSince = cached. getResponseHeader( &quot;Last-Modified&quot;); ifModifiedSince = (ifModifiedSince) ? ifModifiedSince : new Date( 0); // January 1, 1970 request. open( &quot;GET&quot;, url, false); request. setRequestHeader( &quot;If-Modified-Since&quot;, ifModifiedSince); request. send( &quot;&quot;); if(request. status == 304) { request = cached; } }
  • 15.
    JavaScript Libraries Agood library will abstract away the ugly details of cross-platform code. Prototype (the first major lib) jQuery (write less, do more) Script.aculo.us (the prettiest) YUL! (the yahoo!iest) Etc…
  • 16.
    jQuery < html> < head > < script type= &quot;text/javascript&quot; src= &quot;jquery.js&quot;></ script> <script type= &quot;text/javascript&quot;> // Your code goes here </ script> </ head > < body > < a href = &quot;http://jquery.com/&quot; >jQuery</ a > </ body > </ html >
  • 17.
    jQuery Selector Syntax$(‘#elementid’) $(‘.classname’) $(‘#content a.link’)
  • 18.
  • 19.
    Document Ready Not“onload”! $(document).ready(function(){ // Your code here });
  • 20.
    Local Example <html > < head > < script type= &quot;text/javascript&quot; src= &quot;http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js&quot;></ script> <script type= &quot;text/javascript&quot;> $( document).ready( function() { $( &quot;#content&quot;). click( function() { alert( &quot;hi&quot;); }); }); </ script> </ head > < body > < div id = &quot;content&quot; >Did anyone go to Denny's today?</ div > </ body > </ html >
  • 21.