Web Analytics Made Easy -
StatCounter XML / JavaScript problem - CodingForum

Announcement

Collapse
No announcement yet.

XML / JavaScript problem

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • XML / JavaScript problem

    Hi there,

    I only just learning JavaScript and have a question about a bit of script I am playing with. I am basically trying to access XML elements (RSS feed) through a bit of PHP. Everything is working fine when trying to access the <title> and <link>, but when I get to <description> everything breaks down...

    If I comment out this line var theDescription = descriptions[j].childNodes[0].nodeValue; and <p>' + theDescription + '</p>' then the script works fine and displays the titles and the links of the feed, but with those lines in it all goes to pot.

    Can anyone tell me what I am doing wrong here?

    (By the way, don't worry about my method of getting the feed in, using GET etc. This script is part of an AJAX tutorial and I am just messing with it, trying to make it display the title, link, and description from the rss feed)


    Here is my html/JavaScript:

    Code:
    <html><head><title>AJAX XML Example</title>
    </head><body>
    <h2>Loading XML content into a DIV</h2>
    <div id='info'>This sentence will be replaced</div>
    <script>
    
    nocache = "&nocache=" + Math.random() * 1000000
    url = "rss.news.yahoo.com/rss/topstories"
    request = new ajaxRequest()
    request.open("GET", "xmlget.php?url=" + url + nocache, true)
    out = "";
    
    request.onreadystatechange = function()
    {
    	if (this.readyState == 4)
    	{
    		if (this.status == 200)
    		{
    			if (this.responseXML != null)
    			{
    				titles = this.responseXML.getElementsByTagName('title')
    				links = this.responseXML.getElementsByTagName('link')
    				
    				descriptions = this.responseXML.getElementsByTagName('description'); 
    				
    				for (j = 0 ; j < titles.length ; ++j)
    				{
    					var theTitle = titles[j].childNodes[0].nodeValue;
    					var theLink = links[j].childNodes[0].nodeValue;
    					var theDescription = descriptions[j].childNodes[0].nodeValue;
    
    					out += '<p><strong>' + theTitle + '</strong><br /><a href="' + theLink + '">' + theLink + '</a></p><p>' + theDescription + '</p>' ;
    					
    				}
    				document.getElementById('info').innerHTML = out	
    			}
    			else alert("Ajax error: No data received")
    		}
    		else alert( "Ajax error: " + this.statusText)
    	}
    }
    
    request.send(null)
    
    function ajaxRequest()
    {
    	try
    	{
    		var request = new XMLHttpRequest()
    	}
    	catch(e1)
    	{
    		try
    		{
    			request = new ActiveXObject("Msxml2.XMLHTTP")
    		}
    		catch(e2)
    		{
    			try
    			{
    				request = new ActiveXObject("Microsoft.XMLHTTP")
    			}
    			catch(e3)
    			{
    				request = false
    			}
    		}
    	}
    	return request
    }
    </script></body></html>
    Here is the PHP:

    Code:
    <?php // xmlget.php
    if (isset($_GET['url'])) {
    	header('Content-Type: text/xml');
    	echo file_get_contents("http://".sanitizeString($_GET['url']));
    }
    
    function sanitizeString($var) {
    	$var = strip_tags($var);
    	$var = htmlentities($var);
    	return stripslashes($var);
    }
    ?>
Working...
X