AJAX source

I’ve decided that with my new interest in this site and web standards I’d write an article based on web standards for the beginner. A simplified version of the xhtml vs. html argument , doc types, and writing valid code. Which should become available in the coming weeks.

The topic has required a lot of reasearch on the subject, which was necessary based on all the unfounded arguments floating around ont he web. Not to mention just a general misunderstanding of xhtml and html. It seems as if neither side really understands the other (akin to a generational gap between a grandparent and their grandchild) to the point where they will make wild assumptions about the other markup langauge.

I have reduced some of the functionality of the ajax script before I end up removing it all together. I’ll paste it here so that you can see it in it’s entirety if I do completely remove it. The httprequest object creation is based off the script I found at w3schools.

function createAjax() {
  var xmlHttp;
  if (window.XMLHttpRequest) {
    xmlHttp=new XMLHttpRequest();
  }
  else if(window.ActiveXObject) {
    try {
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e) {
      try {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e) {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  }
  return xmlHttp;
}

function getType(name) {
  xmlHttp=createAjax();
  xmlHttp.onreadystatechange=function() {
    if(xmlHttp.readyState==4) {
      document.getElementById("content").innerHTML=xmlHttp.responseText;
    }
  }
  xmlHttp.open("GET",name + ".html",true);
  xmlHttp.send(null);
}

Quite simply I call getType() with an onClick() event on one of my links, passing it the content type I want. I.E. if I want about.html, I just call getType(‘about’). Then an HttpRequest is created, and the responses is placed inside my content div tag.

I eventually plan on removing this completely, but will instead start using it for comment areas on my articles (or whatever else I feel needs it). This initial implementation was simply a demonstration for myself and my friends. I was also interested in how standards compliant it was. That’s about all for now, so until later.

Be Sociable, Share!
This entry was posted in coding. Bookmark the permalink.

Comments are closed.