Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 1st, 2012  |  syntax: None  |  size: 2.27 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /* A RSS reader for Zendesk */
  2.  
  3. <div id="feedContent">
  4.        
  5. </div>
  6. <script type="javascript">
  7.          
  8. Event.observe(window, 'widgets:load', function() {
  9.  
  10. truncate = function(text, length, ellipsis) {    
  11.  
  12.     // Set length and ellipsis to defaults if not defined
  13.     if (typeof length == 'undefined') var length = 100;
  14.     if (typeof ellipsis == 'undefined') var ellipsis = '...';
  15.  
  16.     // Return if the text is already lower than the cutoff
  17.     if (text.length < length) return text;
  18.  
  19.     // Otherwise, check if the last character is a space.
  20.     // If not, keep counting down from the last character
  21.     // until we find a character that is a space
  22.     for (var i = length-1; text.charAt(i) != ' '; i--) {
  23.         length--;
  24.     }
  25.  
  26.     // The for() loop ends when it finds a space, and the length var
  27.     // has been updated so it doesn't cut in the middle of a word.
  28.     return text.substr(0, length) + ellipsis;
  29. }
  30.          
  31.  
  32.        
  33.         $j.get('/proxy/direct?url=http://rss.slashdot.org/Slashdot/slashdot', function(d) {
  34.  
  35.                
  36.                 $j(d).find('item').each(function(index) {
  37.  
  38.                         //name the current found item this for this particular loop run
  39.                         var $item = $j(this);
  40.                         // grab the post title
  41.                         var title = $item.find('title').text();
  42.  
  43.                         // grab the post's URL
  44.                         var link = $item.find('link').text();
  45.                         // next, the description the second value sets the number of characters to display the 3rd value is what you want the ellipse to be
  46.                         var description = truncate($item.find('description').text(), 200, '...');
  47.                         //don't forget the pubdate
  48.                         var pubDate = $item.find('pubDate').text();
  49.  
  50.                         // now create a var 'html' to store the markup we're using to output the feed to the browser window
  51.                         var html = "<div class=\"rssentry\"><h2 class=\"rsspostTitle\">" + title + "<\/h2>";
  52.                         html += "<em class=\"rssdate\">" + pubDate + "</em>";
  53.                         html += "<p class=\"rssdescription\">" + description + "</p>";
  54.                         html += "<a href=\"" + link + "\" target=\"_blank\">Read More >><\/a><\/div>";
  55.  
  56.  
  57.                         //put that feed content on the screen!
  58.                         $j('#feedContent').append(html);
  59.  
  60.                         // Set the number of items you want displayed remember the count starts at zero
  61.                         if(index > 1) {return false;}  
  62.  
  63.                 });
  64.         });
  65.  
  66.  
  67. });
  68.  
  69. </script>