Jump to content

Trouble With Js String.


migroo

Recommended Posts

Okay I am trying to get the browser to load an xml doc and this is the code I am using:

      <script type="text/javascript">	xmlDoc=loadXMLDoc("/forum_stats.xml");   for (t=0;t<=num1(xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue);t++)   var idn= t;   var fl="/forum_links/F_E";   var fx=".xml";   xmlDoc=loadXMLDoc("/forum_links/F_E " + idn + " .xml");   document.write('<tr><td>');   document.write('Icon');   document.write('</td>');   document.write('<td>');   document.write(xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue);   document.write('</td>');   document.write('<td>');   document.write('Auther');   document.write('</td>');   document.write('<td>');   document.write(idn);   document.write('</td>');   document.write('</tr>');   }	</script>

I know the first part of the code works every thing breaks when I try to load the second xml doc using the string.I cant find where it tells in the tutorials about strings but the only thing I can find is where it talks about object strings.Well I hope some one can help me. Thanks

Link to comment
Share on other sites

What makes you think it's a string problem? This code looks wrong to me:

	xmlDoc=loadXMLDoc("/forum_stats.xml");   for  (t=0;t<=num1(xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue);t++)	var idn= t;   var fl="/forum_links/F_E";   var fx=".xml";	xmlDoc=loadXMLDoc("/forum_links/F_E " + idn + " .xml");

Your loop uses a value from the first xmlDoc in its conditional statement. Your num1() function tries to access the first xmlDoc every time the loop is executed. But your loop also overwrites the first xmlDoc and replaces it with a new object. So the conditional statement is trying to read a value that probably does not exist. The loop will break the first time it is executed.If you REALLY need two separate request objects, assign them to unique variables.But it seems to me you are only using the first object to get the length value? In that case, assign that value to a variable bfore your loop begins, and put that variable in your loop condition. (You should do that for all loops anyway. It is more efficient.) I mean something like this:

xmlDoc=loadXMLDoc("/forum_stats.xml");var len = num1(xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue);   for   (t=0;t <= len;t++)

Link to comment
Share on other sites

Okay I put the load length number into a variable:

<script type="text/javascript">	xmlDoc=loadXMLDoc("/forum_stats.xml");var len = num1(xmlDoc.getElementsByTagName("total")[0].childNodes[0].nodeValue);   for   (t=1;t <= len;t++)   {   tv= t;   xmlDoc=loadXMLDoc('/forum_links/F_E'+tv+'.xml')var name = document.write(xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue);   document.write('<tr><td>');   document.write('Icon');   document.write('</td>');   document.write('<td>');   document.write(name);   document.write('</td>');   document.write('<td>');   document.write('Auther');   document.write('</td>');   document.write('<td>');   document.write('1');   document.write('</td>');   document.write('</tr>');   }     </script>

for sum reason it wants to load all the files at once. Here are all the xml files:forum_stats.xml:

<?xml version='1.0'?><stats>	 <total>3</total>	 </stats>

F_E1.xml:

<?xml version='1.0'?><info>   <icon>New!</icon>   <name>Help me with javascript images!</name>   <auther>Tamuj</auther>   <id>1</id></info>

F_E2.xml:

<?xml version='1.0'?><info>   <icon>New!</icon>   <name>I need to know what is your name?</name>   <auther>Tamuj</auther>   <id>2</id></info>

F_E3.xml:

<?xml version='1.0'?><info>   <icon>Question?</icon>   <name>Do you like the forum?</name>   <auther>Tamuj</auther>   <id>3</id></info>

It just loads all the names puts them in one line at the top of the screen and where they are supposed to appear it just writes Undefined.Here is a picture:undefined.pngAny ideas what I can do to fix this problem. And thanks for the help.

Link to comment
Share on other sites

Do you have a link to this page? If not, can you post the code for the whole page? I have to ask also: Why do you have all these very small XML files? Wouldn't it be easier to combine them into one file? That's sort of the point. And it would keep you from making all these AJAX requests, which is very inefficient.

Link to comment
Share on other sites

I would suggest trying to clean the code first... what's with the num1() function anyway?Try it like this:

<script type="text/javascript">	var xmlDoc = loadXMLDoc('/forum_stats.xml');	var len = new Number(xmlDoc.getElementsByTagName('total')[0].childNodes[0].nodeValue);	if (len != NaN)	{		for   (var t=1;t <= len;t++)		{			var tv= t;			xmlDoc = loadXMLDoc('/forum_links/F_E'+tv+'.xml');			var name = xmlDoc.getElementsByTagName('name')[0].childNodes[0].nodeValue;			document.write('<tr><td>');			document.write('Icon');			document.write('</td>');			document.write('<td>');			document.write(name);			document.write('</td>');			document.write('<td>');			document.write('Auther');			document.write('</td>');			document.write('<td>');			document.write('1');			document.write('</td>');			document.write('</tr>');		}	}</script>

And I'd also second the suggestion on putting all the files as one... unless perhaps there's a chance this one file would grow with 200+ elements.

Link to comment
Share on other sites

Okay here is a link: My web pageI am new to this so I am tying to learn. So if you can tell me a better way to do this I would be very happy.I have not yet created it yet but I was planing on creating a area to post every new post would have at least 2 xml files one for the link in the main page ( thats the part I am working on now) and one with the content of the post. I realize this is a very clumsy way of doing thing so if there is a better way please let me know.

Link to comment
Share on other sites

Okay here is a link: My web pageI am new to this so I am tying to learn. So if you can tell me a better way to do this I would be very happy.I have not yet created it yet but I was planing on creating a area to post every new post would have at least 2 xml files one for the link in the main page ( thats the part I am working on now) and one with the content of the post. I realize this is a very clumsy way of doing thing so if there is a better way please let me know.
Any server side way would be better. If you're going to let users post, you're going to need it anyway, so there's no reason not to use it to also change the links to the posts, as well as displaying the posts. The best way is to use a database for all of that, but XML files are fine too... if you don't have other choise especially. Still, you should manage these things on the server.Anyhow, after cleaning up your code, it appears the document.write() upon the creation of the "name" variable is your main issue.
Link to comment
Share on other sites

Okay so I should try using php instead? I will give it a go. Thank you very much for all your help.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...