SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript Code Design Question

    I am new to JavaScript and trying to learn more about programming in general. I have taken a VERY basic JavaScript class in college but learned very little. I have looked at some code examples from various sources on the Internet and diagrammed the logic in an attempt to learn. I have tried to make changes to the code in an attempt to learn more by making the functions more generic so they could handle more than one request.

    Here is the code example that I discovered on the Internet and have modified in an attempt to learn more from.

    Code:
    <script language="javascript"  type="text/javascript">
    
    // Variable which holds the rss feed.  This will have to be modified once I get the 
    // correct logic working
    Backend = 'http://www.atomicrod.info/detroitsportsthatmatter/index.php?/feeds/index.rss1';
    // Function to create the xmlhttp request
    function RSSRequestObject()
    {
    	if (window.XMLHttpRequest) 
    	return new XMLHttpRequest();
    
    	if (window.ActiveXObject)
    	return new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    // Function which will taket he xmlhttp request and send it.  Sets the state change
    // Event handler
    function RSSRequest(SectionRequest)
    {
    	HideShow('status');
    
    	document.getElementById("status").innerHTML = "Requesting data ...";
    	SectionRequest.open("GET", Backend, true);
    	SectionRequest.send(null); 
      	if (SectionRequest.onreadystatechange) ReqChange(SectionRequest);
      	//RSSRequestObject = false;
    }
    
    // Function which trips when Event Handler fires and passes the request object
    function ReqChange(RequestObject)
    {
    	if (RequestObject.readyState==4) 
    	{
    		if (RequestObject.responseText.indexOf('invalid') == -1) 
    		{
    				var node = RequestObject.responseXML.documentElement; 
    				var channel = node.getElementsByTagName('channel').item(0);
    				var title = channel.getElementsByTagName('title').item(0).firstChild.data;
    				var link = channel.getElementsByTagName('link').item(0).firstChild.data;
    			
    				content = '<div class="channeltitle"><a href="'+link+'">'+title+'</a></div><ul>';
    				var items = channel.getElementsByTagName('item');
    				for (var n=0; n < items.length; n++)
    				{
    					var itemTitle = items[n].getElementsByTagName('title').item(0).firstChild.data;
    					var itemLink = items[n].getElementsByTagName('link').item(0).firstChild.data;
    					content += '<li></font><a href="'+itemLink+'">'+itemTitle+'</a></li>';
    				}
    				content += '</ul>';
    				document.getElementById("ajaxreader").innerHTML = content;
    				document.getElementById("status").innerHTML = "Done.";
    			
    		}
    		else
    		{
    			document.getElementById("status").innerHTML = "<div class=error>Error requesting data.<div>";
    		}
    	}
    }
    
    function RSSFeeds()
    {
    	Sports()
    	Politics()
    	MainSite()
    	OffSite()
    }
    
    function Sports()
    {
    	var RSSSports = RSSRequestObject();
    	RSSRequest(RSSSports);
    }

    I beleive the problem is this line of code:
    if (SectionRequest.onreadystatechange) ReqChange(SectionRequest);

    I am not sure if I'm using the event handler proplery or the fact that I'm taking a variable created in one function and passing it to another is causing the problem.

    What happens with the code is I get requesting data, but it never receives the feed back.

    If anyone can be of any help on what logical/language mistakes I've made I'd appriciate it.

    Thanks for the help.

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Location
    London, UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SectionRequest.onreadystatechange is a function that you're suposed to override. Consider something along these lines

    PHP Code:
      SectionRequest.onreadystatechange = function() {
            if (
    xmlHttpReq.readyState == 4) {
                switch (
    xmlHttpReq.status) {
                    case 
    200:
                        
    callback(xmlHttpReq)
                        break
                    case 
    404:
                        
    alert('Error: Not Found.  ' url)
                        break;
                    default:
                        
    document.body.innerHTML xmlHttpReq.responseText;
                        break;
                }
            }
        }
    function 
    callback(res) {
        
    alert(res.responseText);

    Secondly, your Backend variable starts with 'http'. Since AJAX only works on the same domain as the javascript, you might as well define the variable as
    Backend = 'index.php?/feeds/index.rss1';
    before you forget an get strange errors about security.

    Good luck!

  3. #3
    SitePoint Member
    Join Date
    Jul 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help Peterbe. That is working much better.

    In trying to understand it much better, is the reason it was failing is because I created a variable inside one function and tried to pass that variable to another ?

    Thanks again for the help and the security tips. I really appriciate the help.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •