Jump to content

AJAX And it's Uses


barkermn01

Recommended Posts

Forget the Name AJAX(asynchronous Javascript and XML)

This is old and what it was made for but the power is so mutch more

 

The Best way to call AJAX

function GetXmlHttpObject(){
var xmlHttp=null;
try{
		xmlHttp=new XMLHttpRequest();
}
catch (e){
	//Internet Explorer
	try{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e){
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
}
return xmlHttp;
}

This Check for the calls to return if the dont or return false then they fail

xmlHttp=new XMLHttpRequest(); is for All None IE Browsers that support AJAX

xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); is for IE 5 and 6

xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); is for IE7 onwards

 

So we now have a check for witch browser they are using and the way to call AJAX but we need to call it

function getPage(url){ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
	alert ("Browser does not support HTTP Request");
	return;
}
url=url+"?sid="+Math.random(); // fix to keep IE from cacheing the page

xmlHttp.open("GET",url,true);

xmlHttp.onreadystatechange=function() {
	if (xmlHttp.readyState == 4){
		// Your Code to do when you have the result
	}else{
		// Your Code to do when you dont have your page loaded so a nice laoding image works
	} 
}

xmlHttp.send(null);
}

This function will load what ever page you want and will not cash it

 

That is the basics of AJAX and compient for all browsers,

 

But back to my title we dont want to use AJAX for XML well unless using it for an RSS fead

 

So what dose AJAX Return?

Well AJAX has the power to return any text vaule so a web page source loads and you just get javascript to right it to the page so there is one thing what about you want changeing values to be returned, say some dates

 

dates.html

01/01/2001;02/02/2001;03/03/2001;04/04/2001;05/05/2001;06/06/2001;07/07/2001;08/08/2001;

 

So what can AJAX do with that

Well this is a demo

function GetXmlHttpObject(){
var xmlHttp=null;
try{
		xmlHttp=new XMLHttpRequest();
}
catch (e){
	//Internet Explorer
	try{
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e){
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
}
return xmlHttp;
}
function getPage(url){ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
	alert ("Browser does not support HTTP Request");
	return;
}
url=url+"?sid="+Math.random(); // fix to keep IE from cacheing the page

xmlHttp.open("GET",url,true);

xmlHttp.onreadystatechange=function() {
	if (xmlHttp.readyState == 4){
		getPage('dates');
		var data = xhr.responseText.split(/\s?\;\s{0,3}/g);
		var amount1 = data.count();
		var i;
		while(i != amount1)
		{
			 document.write(data[i]+'<br />');
		}
	}else{
		// Your Code to do when you dont have your page loaded so a nice laoding image works
	} 
}

xmlHttp.send(null);
}

 

This will then list the dates but this like i said RSS Feeds can stay upto date with the use of AJAX But that is uses XML Just be creative with AJAX and it can work for you

Link to comment
https://forums.phpfreaks.com/topic/122864-ajax-and-its-uses/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.