Replacing code onload using Javascript

Discussion in 'JavaScript and AJAX' started by chzdanish, Oct 14, 2009.

  1. chzdanish

    chzdanish New Member

    Joined:
    Oct 14, 2009
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    I have a page which uses RSS2JS to display an RSS feed. When the feed contains items, all is well. But when the feed does not contain any items, no content is displayed. It would be preferable to me if RSS2JS would write something like "There are no announcements at this time. Check back later!" rather than leaving a blank spot on the page.

    RSS2JS generates code that looks like this when the feed is empty.
    Code:
    <div class="rss-box"><ul class="rss-items"></ul></div>
    I thought it might be possible to use Javascript to replace the <ul> tags with something like this after loading:

    Code:
    [FONT=Tahoma]<div class="rss-box"><ul class="rss-items"><li>There are no announcements at this time.  Check back later!</li></ul></div>
    [/FONT]

    Sadly, I know next to nothing about Javascript. Any suggestions? I'd be happy to credit you in the code, for what that's worth!:nice:
     
  2. nimesh

    nimesh New Member

    Joined:
    Apr 13, 2009
    Messages:
    769
    Likes Received:
    20
    Trophy Points:
    0
    Occupation:
    Oracle Apps Admin
    Location:
    Mumbai
    Home Page:
    http://techiethakkar.blogspot.com
    Is that the only UL tag in the document?

    If yes, then you can use the JS Function getElementsByTagName()

    Check the code below, if it meets your requirement.

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
      <title> new document </title>
      <script type="text/javascript">
    		function  checkFeedCount(){
    			var x1=document.getElementsByTagName('ul');
    			var x2=document.getElementsByTagName('li');
    			if (x2.length < 1){
    				x1[0].innerHTML="<li>There are no announcements at this time.  Check back later!</li>";
    			}
    		}
      </script>
     </head>
     <body onLoad="checkFeedCount()">
      <div class="rss-box"><ul class="rss-items"></ul></div>
     </body>
    </html>
    
     
    shabbir likes this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice