- JavaScript and XML
- Web browser technology
- Open web standards
- Browser and platform independent
- Better and faster Internet applications
- XML and HTTP Requests
AJAX = Asynchronous JavaScript And XML
The
With
AJAX Is A Browser Technology
The technology makes Internet applications smaller, faster and more user friendly.
AJAX Is Based On Open Standards
- JavaScript
- XML
- HTML
- CSS
The open standards used in
AJAX Is About Better Internet Applications
Web applications have many benefits over desktop applications, they can reach a larger audience, they are easier to install and support, and easier to develop.
However, Internet applications are not always as "rich" and user-friendly as traditional desktop applications.
With
You Can Start Using AJAX Today
There is nothing new to learn.
Most existing web applications can be rewritten to use
AJAX Uses XML And HTTP Requests
A traditional web application will submit input (using an HTML form) to a web server. After the web server has processed the data, it will return a completely new web page to the user.
Because the server returns a new web page each time the user submits input, traditional web applications often run slowly and tend to be less user friendly.
With
The preferred way to communicate with the server is by sending data as XML (but other methods can be used).
You will learn more about how this is done in the next chapters of this tutorial.
AJAX Example
In the
Type a Name in the Box Below
Suggestions:
Example Explained - The HTML Form
The form above has the following HTML code:
First Name: onkeyup="showHint(this.value)">
|
As you can see it is just a simple HTML form with an input field called "txt1".
The paragraph below the form contains a span called "txtHint". The span is used as a placeholder for hints retrieved from the web server.
When the user inputs data, a function called "showHint()" is executed. The execution of the function is triggered by the "onkeyup" event. In other words: Each time the user moves his finger away from a keyboard key inside the txt1 field, the function showHint is called.
Example Explained - The showHint() Function
The showHint() function is a very simple JavaScript function placed in the section of the HTML page.
The function contains the following code:
function showHint(str) { if (str.length > 0) { var url="gethint.asp?sid="+Math.random()+"&q="+str xmlHttp=GetXmlHttpObject(stateChanged) xmlHttp.open("GET", url , true) xmlHttp.send(null) } else { document.getElementById("txtHint").innerHTML="" } } |
The function executes every time a character is entered in the input field.
If there is some input in the text field (str.length > 0) the function executes the following:
- Creates an XMLHTTP object
- Sends an HTTP request to the file "gethint.asp" on the server
- Tells the XMLHTTP object to execute the stateChanged() function when the HTTP triggers a change.
Example Explained - The stateChanged() Function
The stateChanged() function contains the following code:
function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById("txtHint").innerHTML=xmlHttp.responseText } } |
The stateChanged() function executes every time the state of the XMLHTTP object changes.
When the state changes to 4 (or to "complete"), the content of the txtHint span is filled with the response text.
AJAX Example - AJAX Source
The source code below belongs to the
You can copy and paste it, and try it yourself.
The AJAX HTML Page
This is the HTML page. It contains a simple HTML form and a link to a JavaScript.
First Name: onkeyup="showHint(this.value)">
|
The JavaScript code is listed below.
The AJAX JavaScript
This is the JavaScript code, stored in the file "clienthint.js":
var xmlHttp
function showHint(str) { if (str.length > 0) { var url="gethint.asp?sid=" + Math.random() + "&q=" + str xmlHttp=GetXmlHttpObject(stateChanged) xmlHttp.open("GET", url , true) xmlHttp.send(null) } else { document.getElementById("txtHint").innerHTML="" } }
function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById("txtHint").innerHTML=xmlHttp.responseText } }
function GetXmlHttpObject(handler) { var objXmlHttp=null
if (navigator.userAgent.indexOf("Opera")>=0) { alert("This example doesn't work in Opera") return } if (navigator.userAgent.indexOf("MSIE")>=0) { var strName="Msxml2.XMLHTTP" if (navigator.appVersion.indexOf("MSIE 5.5")>=0) { strName="Microsoft.XMLHTTP" } try { objXmlHttp=new ActiveXObject(strName) objXmlHttp.onreadystatechange=handler return objXmlHttp } catch(e) { alert("Error. Scripting for ActiveX might be disabled") return } } if (navigator.userAgent.indexOf("Mozilla")>=0) { objXmlHttp=new XMLHttpRequest() objXmlHttp.onload=handler objXmlHttp.onerror=handler return objXmlHttp } } |
The AJAX Server Page
The server page called by the JavaScript, is a simple ASP file called "gethint.asp".
The page is written in VBScript for an Internet Information Server (IIS). It could easily be rewritten in PHP, or some other server language.
It just checks an array of names and returns the corresponding names to the client:
<% dim a(30) a(1)="Anna" a(2)=" a(3)="Cinderella" a(4)="Diana" a(5)="Eva" a(6)="Fiona" a(7)="Gunda" a(8)="Hege" a(9)="Inga" a(10)="Johanna" a(11)="Kitty" a(12)="Linda" a(13)="Nina" a(14)="Ophelia" a(15)="Petunia" a(16)="Amanda" a(17)="Raquel" a(18)="Cindy" a(19)=" a(20)="Eve" a(21)="Evita" a(22)="Sunniva" a(23)="Tove" a(24)="Unni" a(25)="Violet" a(26)="Liza" a(27)=" a(28)="Ellen" a(29)="Wenche" a(30)="Vicky"
q=ucase(request.querystring("q"))
if len(q)>0 then hint="" for i=1 to 30 if q=ucase(mid(a(i),1,len(q))) then if hint="" then hint=a(i) else hint=hint & " , " & a(i) end if end if next end if
if hint="" then response.write("no suggestion") else response.write(hint) end if %> |
AJAX Database Example
In the
Select a Name in the Box Below
Customer info will be listed here.
AJAX Example Explained
The example above contains a simple HTML form and a link to a JavaScript:
Select a Customer:
|
As you can see it is just a simple HTML form with a drop down box called "customers".
The paragraph below the form contains a div called "txtHint". The div is used as a placeholder for info retrieved from the web server.
When the user selects data, a function called "showCustomer()" is executed. The execution of the function is triggered by the "onchange" event. In other words: Each time the user change the value in the drop down box, the function showCustomer is called.
The JavaScript code is listed below.
The AJAX JavaScript
This is the JavaScript code stored in the file "selectcustomer.js":
var xmlHttp
function showCustomer(str) { var url="getcustomer.asp?sid=" + Math.random() + "&q=" + str xmlHttp=GetXmlHttpObject(stateChanged) xmlHttp.open("GET", url , true) xmlHttp.send(null) }
function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById("txtHint").innerHTML=xmlHttp.responseText } }
function GetXmlHttpObject(handler) { var objXmlHttp=null
if (navigator.userAgent.indexOf("Opera")>=0) { alert("This example doesn't work in Opera") return } if (navigator.userAgent.indexOf("MSIE")>=0) { var strName="Msxml2.XMLHTTP" if (navigator.appVersion.indexOf("MSIE 5.5")>=0) { strName="Microsoft.XMLHTTP" } try { objXmlHttp=new ActiveXObject(strName) objXmlHttp.onreadystatechange=handler return objXmlHttp } catch(e) { alert("Error. Scripting for ActiveX might be disabled") return } } if (navigator.userAgent.indexOf("Mozilla")>=0) { objXmlHttp=new XMLHttpRequest() objXmlHttp.onload=handler objXmlHttp.onerror=handler return objXmlHttp } } |
The AJAX Server Page
The server paged called by the JavaScript, is a simple ASP file called "getcustomer.asp".
The page is written in VBScript for an Internet Information Server (IIS). It could easily be rewritten in PHP, or some other server language.
The code runs an SQL against a database and returns the result as an HTML table:
sql="SELECT * FROM CUSTOMERS WHERE CUSTOMERID=" sql=sql & request.querystring("q")
set conn=Server.CreateObject("ADODB.Connection") conn.Provider="Microsoft.Jet.OLEDB.4.0" conn.Open(Server.Mappath("/db/northwind.mdb")) set rs = Server.CreateObject("ADODB.recordset") rs.Open sql, conn
response.write(" |
0 comments:
Post a Comment