Introduction To AJAX
Introduction To AJAX
Introduction To AJAX
Course Outline
Day 1 I. Introduction II. Getting Started Day 2 III. Using Dynamic Content IV. Applications
I. Introduction
I. Introduction
1. User Experience on the Web
2. What is AJAX?
3. How AJAX Works 4. AJAX Drawbacks 5. Development Tools
response.
A web application is completely idle during load
Applications (RIA)
2. What is AJAX?
Asynchronous JavaScript And XML A group of web development techniques used to create asynchronous web applications
Not a single technology. An HTML-JS-CSS combo Despite the name, XML is not necessarily needed
4. AJAX Drawbacks
AJAX requests are not saved in browser history. Pages with AJAX-loaded content are not easily
4. AJAX Drawbacks
AJAX-loaded content is not accessible to search
engines
The callback-based programming style can lead to
5. Development Tools
A good text editor: Notepad++, gedit or vim have good syntax highlighting and proper indentation Netbeans and Aptana have powerful auto-complete features for JS, HTML and CSS A modern browser with JS console/debugger: Google Chrome (built-in console) Mozilla Firefox (+Firebug add-on)
Getting Started
1. XMLHttpRequest Javascript Object
1. XMLHttpRequest Object
Methods
open(mode, url, asynchronous)
send(params) abort() getAllResponseHeaders() getResponseHeader(header)
Properties
readyState status responseText responseXml
onreadystatechange
1. XMLHttpRequest Object
readyState
status
1. XMLHttpRequest Object
responseText
stores a function to be called automatically every time the readyState value changes
1. XMLHttpRequest Object
open(method, url, asynch, user, pass)
1. XMLHttpRequest Object
send(params)
abort()
4. Lab Exercise
1. First AJAX
simply button will trigger AJAX request to the server, read data.txt and display response in text field
4. Lab Exercise
2. Username validation
we need to validate the username (on the server) and display the result to the user
4. Lab Exercise
Response cannot be plain text, it needs to be: Logically Organized Easy to parse using JavaScript Both XML and JSON fit this criteria
developers these days JavaScript Object Notation A lightweight, language independent, data interchange format. It is derived from the JavaScript scripting language for representing simple data structures and associative arrays, called objects.
comments)
{ status: Jan 25 FTW!, time: Feb 11, 2011 @ 22:00, likes: [Mahmoud Samy, Samir Bahgat, Mohsen Abdallah], comments: [ {user:Sara Ammar, text:Alriiiight!, likes:5}, {user:Mahmoud Samy, text:Go Jan25!!!, likes:4} ] }
2. DOM Manipulations
DOM review document.createElement(tagName) document.createTextNode(text) <element>.appendChild(childNode) <element>.getAttribute(name) <element>.setAttribute(name, value) <element>.insertBefore(newNode, targetNode) <element>.removeAttribute(name) <element>.removeChild(childNode) <element>.replaceChild(newNode, oldNode) <element>.innerHTML
2. DOM Manipulations
DOM Example on adding comments
var commentsDiv = document.getElementById(commentsContainter); for (var i = 0; i < statusObj.comments.length; i++) { var commentHTML = statusObj.comments[i].user + <br/>; commentHTML += statusObj.comments[i].text;
4. Lab Exercise
1. Loading Lists
use API to dynamically load intakes, departments in each intake and students in each department
4. Lab Exercise
A PHP API will be provided for you to use Sample call to get list of intakes:
4. Lab Exercise