Jump to content

Need Help with this AJax


chobo2

Recommended Posts

Hi

 

I am trying to modify some code but their is one thing that is still getting to me. I don't know where the actual writing of the code happens.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
  "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en" dir="ltr">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>My Music AJax example</title>

<script type="text/javascript"><!--
function ajaxRead(file){
  var xmlObj = null;
  if(window.XMLHttpRequest){
      xmlObj = new XMLHttpRequest();
  } else if(window.ActiveXObject){
      xmlObj = new ActiveXObject("Microsoft.XMLHTTP");
  } else {
      return;
  }
  xmlObj.onreadystatechange = function(){
    if(xmlObj.readyState == 4){

	 	var songs =  xmlObj.responseXML.getElementsByTagName('song_name');
		for(var i = 0; i < songs.length; i++)
		{
			updateObj('xmlObj', songs[i].firstChild.data);  
		}
   
     }
    }
    xmlObj.open ('GET', file, true);
    xmlObj.send ('');
  }
  
  function updateObj(obj, data){
   document.getElementById(obj).firstChild.data = data;

}

  //--></script>

  </head>
  <body>
    <h1>Viewing my Music with AJAX</h1>
    <p>This page uses AJax to see a couple of my songs.</p>
    <p id="xmlObj">
     <a href="../../lec 9  & 10/Lesson9 - DOM - SAX - AJAX/ajax/data.xml"
    title="View the XML data." onClick="ajaxRead('data.xml'); this.style.display='none'; return false">View my Music.</a>
    </p>
  </body>
</html>

 

like I added that for loop in xmlObj.readyState  if statement because I want it to grab all the elements of the xml document after it grabs each one I want it to print it out. What is happening right now it is just going through them all and grabbing the last one in the documents.

 

So I am not sure where I should be altering the code so it prints out after each time.

 

Thanks

Link to comment
Share on other sites

I figured it out expect now I don't know to make each one go on its own line. I tried \n but that seems not to be working.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"

  "http://www.w3.org/TR/html4/strict.dtd">

<html lang="en" dir="ltr">

  <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

    <title>My Music AJax example</title>

 

<script type="text/javascript"><!--

function ajaxRead(file){

  var xmlObj = null;

  if(window.XMLHttpRequest){

      xmlObj = new XMLHttpRequest();

  } else if(window.ActiveXObject){

      xmlObj = new ActiveXObject("Microsoft.XMLHTTP");

  } else {

      return;

  }

  xmlObj.onreadystatechange = function(){

    if(xmlObj.readyState == 4){

 

var songs =  xmlObj.responseXML.getElementsByTagName('song_name');

for(var i = 0; i < songs.length; i++)

{

updateObj('xmlObj', songs.firstChild.data); 

}

 

    }

    }

    xmlObj.open ('GET', file, true);

    xmlObj.send ('');

  }

 

  function updateObj(obj, data){

  document.getElementById(obj).firstChild.data += data;

 

}

 

  //--></script>

 

  </head>

  <body>

    <h1>Viewing my Music with AJAX</h1>

    <p>This page uses AJax to see a couple of my songs.</p>

    <p id="xmlObj">

    <a href="../../lec 9  & 10/Lesson9 - DOM - SAX - AJAX/ajax/data.xml"

    title="View the XML data." onClick="ajaxRead('data.xml'); this.style.display='none'; return false">View my Music.</a>

    </p>

  </body>

</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.