iTech College - Internet Technology, Online Education, E-Learning, Technician Training, Tech Tutorials, Distance Education, Virtual Learning AJAX Course: Ajax Basic - www.itechcollege.com AJAX Course: Ajax Basic - www.itechcollege.com
Free Courses
HTML
JavaScript
CSS
ASP
PHP
JSP
C++
ASP.NET
SQL
AJAX
FTP
XML

courses > AJAX > Ajax Basic

AJAX Basic

AJAX uses JavaScript language through HTTP protocol to send and receive data. Let's see an example:

<script type="text/javascript" language="javascript">

Create a function to request HTTP:

function requestHttp(url,posTag){
var httpRequest;

"url" is a variable for file name, "posTag" is another variable for a tag id. For example, "<p id="message">.

Let's check browser type. If the browser is Firefox, Sfari ...:

if(window.XMLHttpRequest){// Mozilla, Safari, ...

httpRequest = new XMLHttpRequest();
if(httpRequest.overrideMimeType){
httpRequest.overrideMimeType('text/xml');
}

Else, if using Microsoft IE:

}else if(window.ActiveXObject){ // IE

try{
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){}
}

}

Get file (see the following function):

getFile(httpRequest,url,posTag);

}

The next step, we send a request to the server for getting data:

function getFile(httpRequest,url,posTag){

if the above http request is failed, then show an alert, and return false;

if(!httpRequest){
alert('Error - Cannot create an XMLHTTP instance');
return false;
}

Else, send a request, and display contents received from server:

httpRequest.onreadystatechange = function(){
showContents(httpRequest,posTag);//function display contents
}
httpRequest.open('GET', url, true);
httpRequest.send('');
}

The third function below is used for showing contents

function showContents(httpRequest,posTag){
if(httpRequest.readyState == 4){
if(httpRequest.status == 200){
alert(httpRequest.responseText);
}else{
alert('Error');
}
}
}

End javascript scripts:

</script>

To call the functions:

<span
style="cursor: pointer; text-decoration: underline"
onclick="requestHttp('file1.txt','message')">
Show contents
</span>

Please note, you need to create a file named "file1.txt" before calling the functions. You can simplly use a text editor to create the file and enter any contents, like "Hello, Free AJAX course from itechcollege.com". When you click the link "Show contents", you will see an alert window pops up, with the contents you entered in the file "file1.txt".

The below is the full source code of the above example:

<html><head>

<script type="text/javascript" language="javascript">
function requestHttp(url,posTag){
var httpRequest;

if(window.XMLHttpRequest){// Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
if(httpRequest.overrideMimeType){
httpRequest.overrideMimeType('text/xml');
}
}else if(window.ActiveXObject){ // IE
try{
httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e){
try{
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e){}
}
}
getFile(httpRequest,url,posTag);
}

function getFile(httpRequest,url,posTag){
if(!httpRequest){
alert('Error - Cannot create an XMLHTTP instance');
return false;
}
httpRequest.onreadystatechange = function(){
showContents(httpRequest,posTag);
}
httpRequest.open('GET', url, true);
httpRequest.send('');
}

function showContents(httpRequest,posTag){
if(httpRequest.readyState == 4){
if(httpRequest.status == 200){
var msg=httpRequest.responseText;

alert(msg);

}else{
alert('Error');
}
}
}
</script>

</head><body>
<span
style="cursor: pointer; text-decoration: underline"
onclick="requestHttp('file1.txt','message')">
Show contents
</span>

</body></html>



  Back to Index Page
 Next Page:  

AJAX Introduction
AJAX Basic
AJAX Display Contents
AJAX Get and Post
AJAX and Database
AJAX and XML
AJAX Dynamically Update Data


Online Education, Online Training for Your Job, Distance Education, E-learning Programs for Your Career!Online Colleges, Online Universities, E-learning & Distance Education for Your Career! Easy Job Training & Online Courses.


   Email A Friend  
 
Earn Online Degree
for Your Job
I found a job after I earned an online degree!
Online classes for finding a Job. Online College, Online University, Degrees, E-learning school, online certificate Earn your
online degree now!
Online Training
for Good Job
Why I work so hard but earn so little? Why I cannot find a high paying job?
Online training for a Good Jobs. Online Degree, E-learning school, online certificate Go to
online school now!

Copyright ©2024, itechcollege.com. All rights reserved
iTechCollege | About iTech | Contact iTech