Friday, June 4, 2010

Javascript-ajax RSS feed reader - using jQuery with jGFeed ...

Check the new version of Ajax feed reader, which is using jQuery with jGFeed.

 <html><head>  
 <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>  
 <script type= "text/javascript" language='javascript'>  
 //<![CDATA[  
 (function($) {  
   $.extend( {  
 jGFeed : function(url, fnk, num, key) {  
       // Make sure url to get is defined  
       if (url == null) return false;  
       // Build Google Feed API URL  
       var gurl = "http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q="+url;  
       if (num != null) gurl += "&num="+num;  
       if (key != null) gurl += "&key="+key;  
       // AJAX request the API  
       $.getJSON(gurl, function(data) {  
         if (typeof fnk == 'function')  
           fnk.call(this, data.responseData.feed);  
         else  
           return false;  
       });  
     }  
   });  
 })(jQuery);  
 var strArr = ['http://www.thinkdigit.com/forum/external.php?type=RSS','http://www.snapfiles.com/feeds/sf20fw.xml',  
        'http://www.download.com/3410-2001-0-10.xml','http://www.download.com/3412-2001-0-10.xml',  
        'http://www.download.com/3412-2003-0-25.xml','http://www.videohelp.com/rss/tools',  
        'http://www.videohelp.com/rss/forum','http://www.pcstats.com/rss/rss.xml','http://feeds.feedburner.com/Techdudes',  
        'http://feeds.feedburner.com/jQueryHowto'];  
 var isXml = true;  
 function loadXml() {  
   var index = Math.floor(Math.random()*strArr.length);  
   getJsonData(strArr[index],'targetDiv');  
 }  
 //loadXml();  
 function getJsonData(dataSource, divId) {  
   $.jGFeed(dataSource,  
   function(feeds) {  
     if (!feeds) {  
       alert('there was an error');  
     }  
     var html = "";  
     for (var i=0;i<feeds.entries.length;i++) {  
       var entry = feeds.entries[i];  
       var title = entry.title;  
       var link = entry.link;  
       var description = entry.contentSnippet;  
       var pubDate = entry.publishedDate;  
       html += "<div class='entry'><h4 class='postTitle'><a class='fancy block' href='" + link + "' target='_blank'>" + title + "</a></h4>";  
       // html += "<em class='date'>" + pubDate + "</em>";  
       var categories = " Lables : ";  
       if (entry.categories && entry.categories.length>0) {  
         categories += entry.categories[0];  
         for (var j=1;j<entry.categories.length;j++) {  
           categories +=", " +entry.categories[j];  
         }  
         html += "<p><b>"+categories+"</b></p>";  
       }  
       html += "<p class='description'>" + description + "</p></div>";  
     }  
     var btn = "<input type='button' value='Reload' onclick='getJsonData(\""+dataSource +"\",\""+divId+"\");' /></br>";  
     $('#'+divId).html(btn + html);  
   },  
   50);  
 }  
 function cleartDiv(divId) {  
   var obj = document.getElementById(divId);  
   if (obj) {  
     //obj.style.display = 'none';  
     obj.innerHTML="";  
   }  
 }  
 function getNewData(url,divId) {  
   if (url!=0) {  
     if (!url) {  
       var index = Math.floor(Math.random()*strArr.length);  
       getJsonData(strArr[index],divId);  
     } else {  
       getJsonData(url,divId);  
     }  
   }  
 }  
 //window.setInterval("getNewData()", 7000); // update the data every 20 mins  
 //]]>  
 </script>  
 </head>  
 <body onload="getNewData('http://www.videohelp.com/rss/tools','targetDiv');">  
     <select onchange="javascript:getNewData(this.options[this.selectedIndex].value,'targetDiv');" size="9" style="width:25%;">  
         <option value='0' selected="selected">Please Select</option>  
         <option value='http://www.thinkdigit.com/forum/external.php?type=RSS'>ThinkDigit.com</option>  
         <option value='http://www.snapfiles.com/feeds/sf20fw.xml'>SnapFiles.com</option>  
         <option value='http://www.articlecity.com/xml/main.xml'>ArticleCity.com</option>  
         <option value='http://www.download.com/3410-2001-0-10.xml'>Download.com populars</option>  
         <option value='http://www.download.com/3412-2001-0-10.xml'>Download.com New</option>  
         <option value='http://www.download.com/3412-2003-0-25.xml'>Download.com 25 New Titles</option>  
         <option value='http://feeds.feedburner.com/Techdudes'>TechDudes</option>  
     </select>  
     <div id="linkDiv" style="width:98%;height:25px;overflow:auto;"/>  
     <div id="targetDiv" style="border:dashed 1px;background:#fafafa;width:70%;height:450px;overflow:auto;font-family:verdana;font-size:10px;"/>  
 </body>  
 </html> 
 
jGFeed: http://plugins.jquery.com/project/jgfeed 

0 Comments: