Digital Solutions & Online Creative 

Alex runs a small digital creative business from an office in London. It's called Outside In Media.

About

SOYREX is a web development and design resource intended as a place for me to share tips and tricks relating to html, css, web design, web development and other internet and web topics. If you like what you read, leave a comment, or send an email. Also, check out my company web site.

This form does not yet contain any fields.
    Search the Archives

    Online Schools

    Currently Reading..
    • Blink: The Power of Thinking Without Thinking
      Blink: The Power of Thinking Without Thinking
      by Malcolm Gladwell

      Blink talks about flash cognition and sub-conscious cognitive activity.. awesome read!

    • Confessions of an Economic Hit Man: The Shocking Story of How America Really Took Over the World
      Confessions of an Economic Hit Man: The Shocking Story of How America Really Took Over the World
      by John Perkins

      Confessions of an Economic Hit Man - i knew the world was a big conspiracy.. but this is a gripping insight into how the world really works.

    Recommended Reading
    • Designing with Web Standards (Voices That Matter)
      Designing with Web Standards (Voices That Matter)
      by Jeffrey Zeldman, Ethan Marcotte
    • Web Standards Creativity: Innovations in Web Design with XHTML, CSS, & DOM Scripting: Innovations in Web Design with XHTML, CSS, and DOM Scripting
      Web Standards Creativity: Innovations in Web Design with XHTML, CSS, & DOM Scripting: Innovations in Web Design with XHTML, CSS, and DOM Scripting
      by C et al Adams
    Tuesday
    Aug252009

    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:

    Final Pipe layout.

    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 :)

    PrintView Printer Friendly Version

    EmailEmail Article to Friend

    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?

    September 8, 2009 | Unregistered CommenterSinead

    @Sinead: have you uploaded what you've done somewhere? if you want to send a link, i'll have a look at it...

    September 10, 2009 | Registered Commenterrex

    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?

    September 20, 2009 | Unregistered CommenterPascal

    ... even with &_callback=? ...

    September 20, 2009 | Unregistered CommenterPascal

    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.

    September 25, 2009 | Unregistered CommenterAllan W.

    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?

    September 26, 2009 | Unregistered CommenterAllan W.

    Got it. Thanks for the code. really helpful.
    nike air shoes

    December 1, 2009 | Unregistered CommenterMostafa

    IE 6 not working pls check

    December 3, 2009 | Unregistered Commenterleo

    Typical Mac dev... doesn't write code that works on ALL browsers, only FF and Safari...

    December 7, 2009 | Unregistered CommenterMobile Web User

    ie6 is deprecated.

    Besides.. it's a proof of concept - if you dont like it.. go write IE6 compatible code to do it yourself?

    December 11, 2009 | Registered Commenterrex

    Dear friend, i also create pipe, nice but there is nothing scroll effect in that .

    Thanks with regards
    yash

    January 20, 2010 | Unregistered Commenteryash

    Good to visit your blog with nice post on it

    March 4, 2010 | Unregistered CommenterRačunovodstvo

    thanks for the great info!!

    March 15, 2010 | Unregistered Commenterwedding dresses

    Thanks for providing the coding for the widget and it is perfectly working good.

    March 27, 2010 | Unregistered Commenterfromchina

    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.

    April 1, 2010 | Unregistered CommenterNatural Makeup

    This coding is very useful and i have tried this

    April 5, 2010 | Unregistered CommenterFile search

    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

    April 16, 2010 | Unregistered CommenterFree Online Games 

    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,

    May 29, 2010 | Unregistered CommenterShimla Hotels

    Thanks for providing the coding for the widget and it is perfectly working good.

    May 29, 2010 | Unregistered Commenterstock chat

    Thanks for providing the coding for the widget and it is perfectly working good

    June 8, 2010 | Unregistered CommenterTelephone Systems

    Thanks for sharing very helpful information.

    June 9, 2010 | Unregistered CommenterBuy Software

    Your providing code are very helpful.

    June 9, 2010 | Unregistered CommenterAdsense Earnings

    hi

    any tips for including a snippet from the actual post content also?

    June 15, 2010 | Unregistered Commenterjonas

    PostPost a New Comment

    Enter your information below to add a new comment.

    My response is on my own website »
    Author Email (optional):
    Author URL (optional):
    Post:
     
    Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
    « Concept: Semantic Markup | Main | Creating an accessible navigation menu from a UL »