Tuesday, August 25, 2009 |
42 Comments | Create a simple Ajax RSS Widget with Jquery + HTML
A medium to advanced user's tutorial for using Jquery to display a set of RSS headlines as links in a widget on your page. There are code samples on this page, however if you want to follow along easier, here is the complete final file (it's all in one HTML file for ease of use - obviously you'll want to separate your JS and CSS out into external files..)
So, suppose you want to embed the headlines from an RSS feed into your site. Assuming you're not so fussed about SEO, but rather are looking for a useful piece of content for your users, you can use Jquery and Yahoo Pipes to easily embed the headlines (and summaries if you like) into your page.
Step 1: Create a Yahoo Pipe for the RSS feed (or feeds) that you want to include.
You need a Yahoo account to use pipes, so if you don't have one, you'll need to get one. Once you have it, just log into the pipes site here: http://pipes.yahoo.com
You want to choose, Sources > Fetch Feed - this will add a Fetch Feed box into your pipe editor, now link this with the Pipe Output. That's it (Pipes are extremely powerful, however this i all we need for this particular example). Here's how the final pipe looks:

Save the Pipe. Go "Back to My Pipes" and get the URL for the new pipe as JSON.
Mine pipe URL is: http://pipes.yahoo.com/pipes/pipe.run?_id=b5c348713f1c84193acf3723d4d148a9&_render=json
Now that you have the JSON url, we can use jquery to render these results into a useful widget on our page.
Firstly, we need to make sure that Jquery is being included on our page. I do this using the google hosted solution, so basically it is loaded from google's servers (and cached). So, in the head of your page (if jquery isn't already there) we add this:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
Step 2: Set up our markup and CSS
Now we create the necessary markup to hold our results. Where you want the widget to appear, we append this:
<div id="rssdata">
<ul class="rss-items"></ul>
<div class="loading">Loading RSS items...</div>
</div>
Obviously you can use whatever ids and classes you like, just remember to change the javascript and CSS that follows so Jquery can find the right elements.
Basic CSS styles:
#rssdata ul.rss-items
{
display: none;
margin: 0;
padding: 0;
}
#rssdata ul.rss-items li
{
display: block;
margin: 0;
padding: 0;
}
#rssdata ul.rss-items a
{
margin: 0;
padding: 0;
display: block;
padding: 2px 6px;
background: #ccc;
color: #333;
text-decoration: none;
border-bottom: 1px solid #eee;
}
#rssdata ul.rss-items a:hover
{
background: #666;
color: #fff;
text-decoration: none;
}
Step 3: Building the Widget using Jquery..
Now, we need to make our progressively enhanced Jquery:
$('#rssdata').ready(function()
{
var pipe_url = 'http://pipes.yahoo.com/pipes/pipe.run?_id=b5c348713f1c84193acf3723d4d148a9&_render=json&_callback=?';
$.getJSON(pipe_url,function(data)
{
$(data.value.items).each(function(index,item)
{
var item_html = '<li><a href="'+item.link+'">'+item.title+'</a></li>';
$('#rssdata ul.rss-items').append(item_html);
});
$('#rssdata div.loading').fadeOut();
$('#rssdata ul.rss-items').slideDown();
});
});
So, in english, we add a ready function to the #rssdata div, it holds the JSON url from our pipe above, with &_callback=? added on the end (this allows cross domain JSONP requests).
And then the ready function simply using jquery's getJSON method to load the results of our pipe and turn them into HTML elements that we then append into our UL.
Once the loaded data has all been appended, we use fadeOut to hide our Loading message and then slideDown to make our UL slide into view nicely.
If you need more help, here's a single HTML page that has all the code in the tutorial in it, for easy use (you can also use this to preview the effect).
That's it. If you need help, leave a comment :)




Reader Comments (42)
Hi Rex. I'm trying to make this work within a small div on my page (about 300px w X 200px H) but no matter what I try it won't. Any ideas?
@Sinead: have you uploaded what you've done somewhere? if you want to send a link, i'll have a look at it...
Hi, when I tried it myself I got 'NULL' in stead of headlines.. Then I looked at outsideinmedia.co.uk/tumblr/downloads/rss_widget.html and found the same result. In IE7 as well as in FF3.5. Is it a Vista thing maybe?
... even with &_callback=? ...
The URL is broken, but I'm going to download and play with this. Looks like a good approach.
So, you must convert the RSS into a JSON first? Interesting.
Greetings - trying this code with my own feed, and it doesn't work either.
http://webcast.palau.org/widget/
Using JSON code:http://pipes.yahoo.com/pipes/pipe.run?_id=fb4bfc940c9662eb6ffdd28f98320ea1&_render=json
When I examine the output of the JSON query, I do see the feed data coming across - I conclude something is up with the parsing. Any suggestions?
Got it. Thanks for the code. really helpful.
nike air shoes
IE 6 not working pls check
Typical Mac dev... doesn't write code that works on ALL browsers, only FF and Safari...
ie6 is deprecated.
Besides.. it's a proof of concept - if you dont like it.. go write IE6 compatible code to do it yourself?
Dear friend, i also create pipe, nice but there is nothing scroll effect in that .
Thanks with regards
yash
Good to visit your blog with nice post on it
thanks for the great info!!
Thanks for providing the coding for the widget and it is perfectly working good.
Does this works in any kind of blog, am finding difficult to fix the error it shows, hope i can fix it with my developer.Though simple and good.
This coding is very useful and i have tried this
Thanks for sharing the code its useful i have tried it
I do tried it for my website and it is a valuable one, I have added my new ideas with Ajax
Just wondering if it correct usage since the above specification says it accepts an html STRING, not a jquery object. It would make sense for it to work,
Thanks for providing the coding for the widget and it is perfectly working good.
a10jun0531
Tiffany Jewelry
Replica tiffany
wholesale crystal
tiffany jewelry
Hermes Jewelry
Bottega Veneta
Gucci Jewelry
DIOR Jewelry
Chic Jewelry
Necklaces Tiffany Jewelry
Replica Handbags
Hermes handbags
LOUIS VUITTON Handbags
HERMES Handbags
PRADA Handbags
Dior Handbags
Replica WALLETS
Louis Vuitton Wallets
Hermes Wallets
Gucci Wallets
replica handbags
replica purses
Hermes handbags
replica watches
breitling replica watches
replica watches
Replica handbags
Gucci Belt
Louis Vuitton Bracelet
Tiffany Jewelry 2010
Replica tiffany Fashion
Hermes Handbags 2010
wholesale crystal Fashion
tiffany jewelry 2010
Hermes Jewelry Fashion
Bottega Veneta 2010
Gucci Jewelry Fashion
DIOR Jewelry 2010
Replica Handbags Fashion
LOUIS VUITTON Handbags 2010
Hermes Handbags Fashion
Prada Handbags 2010
Dior Handbags Fashion
Necklaces Tiffany Jewelry 2010
Bracelets Tiffany Jewelry Fashion
Earrings Tiffany Jewelry 2010
Rings Tiffany Jewelry Fashion
Cuff links Tiffany Jewelry 2010
Money clips Tiffany Jewelry Fashion
Thanks for providing the coding for the widget and it is perfectly working good
Thanks for sharing very helpful information.
Your providing code are very helpful.
hi
any tips for including a snippet from the actual post content also?