barkermn01 Posted September 5, 2008 Share Posted September 5, 2008 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 Quote Link to comment Share on other sites More sharing options...
barkermn01 Posted September 6, 2008 Author Share Posted September 6, 2008 Sorry in the xhr.responseText Should be xmlHttp.responseText Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.