Ajax ticker news feed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • danielm60
    New Member
    • Apr 2010
    • 16

    #1

    Ajax ticker news feed

    I have a newsfeed which is made up of a js ticker that uses a php grab to convert an rss feed and show it in a html page but I have an error ....

    MyElement.getEl imentsByTagName (naming)[0].firstChild is null

    If I click ok it goes away and the ticker works fine.

    Can anyone help me correct the error?
    Attached Files
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    without seeing the code it would be guesswork to give an answer ... basically:

    first i assume that:
    Code:
    MyElement.getElimentsByTagName(naming)[0].firstChild
    is a typo it needs to be:
    Code:
    MyElement.getElementsByTagName(naming)[0].firstChild
    in case it was a typo in the post then the above line refers to a node that doesn't exist ... at least at this moment where it should be retrieved ...

    kind regards

    Comment

    • danielm60
      New Member
      • Apr 2010
      • 16

      #3
      Hi
      here is the js code if it helps

      Code:
      	function getRequest() {
      		var con = new Array();
      		var _ms_XMLHttpRequest_ActiveX = "";
      		var req;
      		if (window.XMLHttpRequest) {
      			req = new XMLHttpRequest();
      		} else if (window.ActiveXObject) {
      			if (_ms_XMLHttpRequest_ActiveX) {
      				req = new ActiveXObject(_ms_XMLHttpRequest_ActiveX);
      			} else {
      				var versions = ["Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP","Microsoft.XMLHTTP"];
      				for (var i = 0; i < versions.length ; i++) {
      					try {
      						req = new ActiveXObject(versions[i]);
      						if (req) {
      							_ms_XMLHttpRequest_ActiveX = versions[i];
      							break;
      						}
      					} catch (objException) {
      								// trap; try next one
      					}
      				}
      			}
      		}
      		con[0]=req;
      		con[1]=_ms_XMLHttpRequest_ActiveX;
      		return con;
      	}
      
      	function loadRSS(url) {
      	//alert("running");
      		try {
      			if ( _ms_XMLHttpRequest_ActiveX1 ) {
      				rssRequest.onreadystatechange = processRSS;
      				rssRequest.open("GET", url, true);
      				rssRequest.send(null);
      			} else {
      				if (rssRequest) {
      					rssRequest.onreadystatechange = processRSS;
      					rssRequest.open("GET", url, true);
      					rssRequest.send(null);
      				}
      			}
      		} catch ( rssRequestException ) {}
      	}
      
      
      	function getChildNode( myElement, naming ) {
      		return myElement.getElementsByTagName(naming)[0].firstChild.nodeValue;
      	}
      
      	function processRSS() {
      		var cycle = 0;
      		try {
      			if (rssRequest.readyState == 4) {
      				if (rssRequest.status == 200) {
      					response = rssRequest.responseXML.documentElement;
      					if ( response ) {
      						var items = response.getElementsByTagName("item");
      						for ( var i = 0 ; i < items.length; i++ )
      						{
      							var title = getChildNode(items[i],"title");
      							var desc = getChildNode(items[i],"description");
      							var link = getChildNode(items[i],"link");
      							rssItems[cycle] = title + " - " + desc;
      							linkItems[cycle] = link;
      							++cycle;
      						}
      					}
      				} else {
      					alert("There was a problem retrieving the XML data:\n" + req.statusText);
      				}
      			}
      		} catch ( jsException ) {alert(jsException.message);}
      	}
      
      	var httpArc2 						= getRequest();
      	var rssRequest						= httpArc2[0];
      	var _ms_XMLHttpRequest_ActiveX1 	= httpArc2[1];
      
      	var rssItems = new Array();
      	var linkItems = new Array();
      
      
      	function openWindow(url){
      		window.open(url);
      	}
      
      
      
      	var storyCount = 0;
      	var itemLen = 0;
      
      	function tickTock(){
      
      		var obj = document.getElementById("scroller");
      
      		var myTimeout = 50;
      		var str;
      		var st = rssItems[storyCount];
      
      		if ( itemLen > st.length ) {
      			storyCount++;
      			if ( storyCount >= rssItems.length  ) {
      				storyCount = 0;
      			}
      			itemLen = 0;
      			myTimeout=2000;
      			setTimeout("tickTock()", myTimeout);
      		} else {
      			++itemLen;
      			str = rssItems[storyCount].toString();
      			str = str.substring(0,itemLen) + "_";
      			obj.innerHTML = "<span class=globalheader>Latest BBC News:</span><br/><a href=\"javascript:openWindow('"+linkItems[storyCount]+"')\" class=headlines>" + str + "</a>";
      			setTimeout("tickTock()", myTimeout);
      		}
      
      	}
      
      	setTimeout("tickTock()", 3000);
      
      
      	loadRSS("/grab.php?http://news.bbc.co.uk/rss/newsonline_world_edition/front_page/rss.xml");
      Last edited by gits; May 14 '10, 03:27 PM. Reason: added code tags

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        so as you might see the error probably comes from one of the lines 62, 63 or 64 where the getChildNode()-method is called. since this method is called on the result of the XMLHttpRequest i suspect that the response might miss an item's subnode or that there is a corrupt node somewhere ... so that:

        Code:
        myElement.getElementsByTagName(naming)
        returns null somewhere on that result. probably you have an empty item or similar.

        kind regards

        Comment

        • danielm60
          New Member
          • Apr 2010
          • 16

          #5
          Hi sorry Im a newb ... so I dont really know what you are say except that
          MyElement.getEl ementsByTagName (naming)[0].firstChild
          means that an element is empty!

          Can you help to fix it ?

          Comment

          • gits
            Recognized Expert Moderator Expert
            • May 2007
            • 5390

            #6
            you would need to check the returned XML structure ... the method fails on a specific node ...

            Comment

            • danielm60
              New Member
              • Apr 2010
              • 16

              #7
              Hi

              How do it do that as the wole ticker is just the 2 .php. files and the js file ?
              See attached

              I think the error comes from line 103 of the js file where the lenghth is null or not an object
              Attached Files

              Comment

              • gits
                Recognized Expert Moderator Expert
                • May 2007
                • 5390

                #8
                just call the app in Firefox with an installed firebug-extension and check the response of the XMLHttpRequest - you might even use Safari and activate the developer tools to check that ... Firebug will then tell you which line would produce the problem.

                from you first post it is obvious that the error occurs in the getChildNode()-method ... which is called from the lines 62-64 in post #3. that is why i assume that the response is corrupt somewhere ... so that any item might miss a title, description or link node.

                Comment

                • danielm60
                  New Member
                  • Apr 2010
                  • 16

                  #9
                  Hi gits,

                  Yes already done that
                  The line is 103 as per earlier posts
                  # if ( itemLen > st.length ) {
                  # storyCount++;
                  # if ( storyCount >= rssItems.length ) {
                  # storyCount = 0;

                  I just dont know how to cure it or get the browser to ignore the problem as the ticker works fine once you click ok to the error
                  see

                  if it helps the rss is

                  Comment

                  • gits
                    Recognized Expert Moderator Expert
                    • May 2007
                    • 5390

                    #10
                    i suspect that the there are more problems ... first ... the two alerts come from the line where you catch the exeption:
                    Code:
                    alert(jsException.message);
                    this is what we already talked about above.

                    the other error seems to come from line:
                    Code:
                    obj.innerHTML = "<span class=globalheader>Latest BBC News:</span><br/><a href=\"javascript:openWindow('"+linkItems[storyCount]+"')\" class=headlines>" + str + "</a>";
                    as this code isn't executed reliably from the page's onload event i suspect that the code runs to ealy and the DOM isn't ready at this moment ... when it runs the second time it finds the obj ... you might return from that function if
                    Code:
                    obj == null
                    or better you create a defined flow for your code ... you should start all DOM operations after! the document's onload event ...

                    Comment

                    • danielm60
                      New Member
                      • Apr 2010
                      • 16

                      #11
                      Oh no ... sounds like a nightmare... it's too much for a newbie like me... is there anyone you can recommend who could fix this for me ?

                      Comment

                      • gits
                        Recognized Expert Moderator Expert
                        • May 2007
                        • 5390

                        #12
                        just be sure to start all DOM related operations not before the documents onload event. this is quite simple ... the document's DOM is reliably ready to use when the following event is fired:

                        Code:
                        <body onload="alert('Documment DOM ready');">
                        so you would just need to call your first line of JavaScript from here and go on with the rest.

                        kind regards

                        Comment

                        • danielm60
                          New Member
                          • Apr 2010
                          • 16

                          #13
                          Hey thanks for your help...

                          So it's just a case of rearranging the existing code code ?

                          It that in the html or the js ?

                          What line number would that start at ?

                          Comment

                          • gits
                            Recognized Expert Moderator Expert
                            • May 2007
                            • 5390

                            #14
                            as you might see the onload-handler is assigned the the document's body element in the above example. it isn't just rearranging ... it even would involve a bit refactoring. you would need to call everything DOM-realted from a function that is then called when the onload fires ... currently you just include scripts and call functions from there. in case the DOM is not ready you will get errors from all methods that would rely on this ... and as i already said a delay is quite unreliable. so in your included scripts you should ensure that no such method is directly called during the include but later on from the onload-handler ...

                            Comment

                            • danielm60
                              New Member
                              • Apr 2010
                              • 16

                              #15
                              Right .... I think this is way above my knowledge base... so it's something I need to get done by a js script code writer... I was just hoping there was a quick fix to the problem

                              Comment

                              Working...