/ Published in: JavaScript
Very basic grab a line of text from a file then display it in an alert box. Uses other functions for loading prepareLinks() and creating the request.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
function basicAJAX(file) {//pass a variable into the function var request = getHTTPObject(); if(request){ request.onreadystatechange = function() { displayResponse(request); }; request.open("GET", file, true);//this is where the var is picked up, the location request.send(null); } } function displayResponse(request) { if(request.readyState == 4){//waits for the complete before execute. if(request.status == 200 || request.status == 304){ alert(request.responseText);//this is what happens once complete //for XML //var data = request.responseXML;//the while document is now in this variable. Travesrse it using the DOM. //createInfo(data);//function runs and references the DOM } else { alert("Something Broke!"); } } } function prepareLinks(){ if(!document.getElementsByTagName) return false; var allLinks = document.getElementsByTagName("a"); for(i=0;i<allLinks.length;i++){ allLinks[i].onclick = function() { basicAJAX(this.href); return false; } } }