simple web design tips.

Create a simple Ajax RSS Widget with Jquery

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

2 years ago

Why i moved my blog to tumblr.

In an attempt to get the content flowing again on my site, i’ve moved (in a seemingly inexplicable backwards step) from using my custom django blog, to using a custom themed tumblr blog.

Why? What the hell were you thinking? i hear you asking…

Well, firstly, tumblr is a simple, hosted solution and it comes with an iphone app, and some simple content classes that i like the look of.

And secondly, i wanted to see if i could get away with it :)

I’ve also implemented 3rd party commenting and a couple of other things. If you’re a new reader, or a past reader… please leave a note and tell me what you hate about the new (hopefully more content rich) site…

2 years ago

Hehe. I love geeky humour… but the funniest thing is that I almost did it, then decided I didn’t wantto put smudges on my screen.

Visit: Internet High Five posted 2 years ago

Down for everyone or just me is a super simple website. Basically, it lets you check if a web server is down from a third party location. The idea being, you panic because you can’t hit your web site, so you go and check it through Down for everyone to see if it’s a problem with your connectivity, or the web server - super useful!

Visit: Down for everyone or just me? posted 2 years ago

Concept: Semantic Markup

The term semantic markup has become another of today’s buzzwords. People always talk about it, but few of them really understand the concept. The problem is unless you’re a philosopher, the idea of semantics is about as meaningful as metaphysics.

So lets try and clarify what semantic markup is and what significance it has to the web designer.

Semantics is the study of meaning. Semantic markup is markup that has meaning, the idea is tha the tags you choose add to the overall meaning of the content you are marking up. The earliest (and simplest) example of this is the argument between strong and b tags. Semantically the strong tag has meaning, it implie strong emphasis, and thus conveys added meaning. Whereas the b tag just implies bold, a purely visual characteristic.

A second example is the markup for the nav of your page. Semanticly it makes more sense to mark the navigation elements up inside a unordered list, because nav is in fact a list of items on the site.

So..

<ul class="nav">
    <li><a href="#">Item 1</a></li>
    <li><a href="#">Item 2</a></li>
    <li><a href="#">Item 3</a></li>
</ul>

..is semantically morencorrect than using DIV tags or even just A tags in a paragraph, like this:

<p>
    <a href="#">Item 1</a> | 
    <a href="#">Item 2</a> | 
    <a href="#">Item 3</a>
</p>

Semantics also allow screen readers and other machine based clients to better understand the meaning of the content in your markup.

Hopefully that clears up the idea.

2 years ago

soyrex. i am rex.

Hi! my name is Alex Holt, some people call me rex. I'm a web designer. I post tips, tricks, tutorials and other tidbits about web design.

recent posts

Loading recent posts...

recent links

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 us a comment :) - if you want to know more, you can email blog [at] soyrex [dot] com. Enjoy your stay.