topical media & game development

talk show tell print

basic-ajax-11-ch11-examp1.htm / htm



  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  
      <title>Checking Courses</title>
      <script type="text/javascript">
          
       function getDoc()
        {         
           var request; 
  
        if (window.XMLHttpRequest) {
            request = new XMLHttpRequest();
         } 
             else if (window.ActiveXObject) {
            request = new ActiveXObject("Microsoft.XMLHTTP");
          }
  
          if(request) {
            request.open("GET", "classes.txt", true); 
  
            request.onreadystatechange = function() 
                    
              if ((request.readyState == 4) && (request.status == 200)) { 
                          var jsonResp = request.responseText;
                          jsonResp = eval("(" + jsonResp + ")");
                          findClass(jsonResp);
              }
              
            } 
  
            request.send(null); 
                    
        }
            
            function findClass(jsonResp) {
            
            for (i=0; i < jsonResp.class1.length; i++) {
            var title = jsonResp.class1[i].title;
            var req = jsonResp.class1[i].req;
            
            var myEl = document.createElement('p');
            var newText = title + " is the name of a course in the Computer Science department.";
        var myTx = document.createTextNode(newText);
        myEl.appendChild(myTx);
        var course = document.getElementById('title');
        course.appendChild(myEl);
            
            if (req == 'yes') {
            var addlText = " This is a required course.";
            var addlText2 = document.createTextNode(addlText);
            myEl.appendChild(addlText2);
            }
            
            else {
        var addlText = " This is not a required course.";
            var addlText2 = document.createTextNode(addlText);
            myEl.appendChild(addlText2);
            }
          }
            
  }  
  </script>
  
  </head>
  
  <body>
  
  <h1>Checking courses</h1>
      <form>
        <input type = "button" id="reqDoc" value = "Check courses" />
      </form>
  <script type="text/javascript">
  var myDoc = document.getElementById('reqDoc');
  myDoc.onclick = getDoc;
  </script>
  <div id="title"></div>
  </body>
  </html>
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.