12_PHP_Lecture
12_PHP_Lecture
AJAX
web Engineering ||
winter 2017
wahidullah Mudaser Lecture 12
[email protected]
AJAX
OUTLINE
What’s Ajax?
JavaScript in 10 minutes
DOM and XMLHttpRequest
Programming Ajax with Mochikit
Ajax Pitfalls
Ajax in gnizr
Asynchronous JavaScript And XML
The idea is to enable interactive Web pages without requiring the browser to
refresh.
Ajax
CSS
XHTML JavaScript
JavaScript in 10 Minutes
Say “hello world”
What’s ‘document’?
Write into HTML <body/>
You can read from and write to the current document via this
‘document’ variable.
Loading JavaScript code
Two ways to load your JS code
Inline within the same HTML document
From a separate file on the server
I love it!
DOM and XMLHttpRequest
DOM
http://flickr.com/photos/13571173@N00/411653343/
An XML DOM (not XHTML)
<bookstore>
<book category=“c1”>
<title lang=“en”>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
</bookstore>
HTML DOM Tree – an example
XMLHttpRequest
Is a specification
Defines an API that provides scripted client functionality for transferring data
between a client and a server.
The XMLHttpRequest object is used to exchange data with a server behind the
scenes.
This means that it is possible to update parts of a web page, without reloading the
whole page. Modify the page content, without
refreshing the page
“Give me some data”
JavaScript
code
“Here, data.”
Web Server
Browser
Use XMLHttpRequest to send and receive data from servers via HTTP
Data sent and received can be any text format, not just XML.
Works over HTTP and HTTPS
All modern browsers (Chrome, IE7+, Firefox, Safari, and Opera) have a built-in
XMLHttpRequest object.
var xhttp;
if (window.XMLHttpRequest) {
xhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
Send a Request To a Server
we use the open() and send() methods of the XMLHttpRequest object:
AJAX exmple
Questions?