Playing with Blogger json feed using jQuery

I used blogger [a.k.a BlogSpot] for sometime and liked it, in the recent time I want complete control over designing and displaying and moved to Drupal hosting and start blogging over there with my own website.

I want to show my old entries from blogger on my new website, then I found blogger provides us rss feeds in xml or json format of the full post, I was happy and start working on it.

First I thought that I would add entries manually, but what if I have hundreds of entries, then I opt to show all recent entries dynamically. I opt jQuery for all  my work.

If you have blogger account then you might have following address:

http://yourname.blogspot.com

and you can find the json feeds at the following url:

http://YOURNAME.blogspot.com/feeds/posts/default?orderby=published&alt=json-in-script&max-results=10

Don’t forget to replace above url with your actual username.

Now I want to show 10 latest published post from blogger to my website, or we can get it in any website, all we need to paste the following code:

<script type="text/javascript">
        $(function() {
            $("#ajaxloader").show();
            var monthname = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
            $.getJSON("http://dnetpro.blogspot.com/feeds/posts/default?orderby=published&alt=json-in-script&max-results=10&callback=?",
            function(data) {
                $.each(data.feed.entry, function(i, postentry) {
                    var posttitle = postentry.title.$t
                    var postsummary = postentry.summary.$t.substring(0, 100);
                    var striphtml = /<\S[^>]*>/g;
                    postsummary = postsummary.replace(striphtml, "")
                    var postdate = postentry.published.$t.substring(0, 10);
                    var postyear = postdate.substring(0, 4);
                    var postmonth = postdate.substring(5, 7);
                    var postday = postdate.substring(8, 10);
                    var datehtml = $("<div class='divblogdate'></div>");
                    $("<div class='monthbg'></div>").html(monthname[parseInt(postmonth,10)-1]).appendTo(datehtml);
                    $("<div class='datebg'></div>").html(postday).appendTo(datehtml);
                    datehtml.appendTo("#posts");
                    $.each(postentry.link, function(j, postlink) {
                        if (postlink.rel == 'alternate') {
                            var linkhtml = $("<div class='divbloglink'></div>");
                            $("<a class='bloglink'></a>").html(posttitle).attr("href", postlink.href).appendTo(linkhtml);
                            $("<p></p>").html(postsummary + "...").appendTo(linkhtml);
                            linkhtml.appendTo("#posts");
                        }
                    });
                });
                $("#ajaxloader").hide("fast");
            });
        });
    </script>

Above code will fetch 10 latest blog entries of mine and inject the manipulated feed into my ASP.NET page. There is no need to have a ASP.NET page for getting blogger feed, you can get it in simple html page and don’t forget to include latest jquery reference in your page.

My html markup is also simple, here it is:

<h2>
	.Net Pro Blog
</h2>
<p>Recent blog post. enjoy!</p>
<div id="ajaxloader" style="display: none;">
	<img alt="Loading..." src="images/spinner.gif" />&nbsp;<span>Loading...</span>
</div>
<div id="posts">
</div>

Now return to the above jQuery code and understand it. I started with jQuery’s document.ready() function, I used it’s simplest shortcut. In that vary function I called show() method which show’s up hidden progress indicator, then I declared array of months name. After that actual calling to the blogger feed is done with the help of jQuery’s getJSON method which have following general syntax:

jQuery.getJSON( url, [data], [callback] )

I provided above defined url in the first parameter, which is straight forward, On the second parameter I’ve anonymous function which gets and generate the desired output.

You can find more about the blogger api at Google.

Live demo

You can do more with it at your own.

Hope this works!

Comments

Bet on UFC

I often read your blog, i think you are good writer, keep us posting

teleskopy astronomiczne

I just want to say that your blog is full of interesting articles, keep us posting

Luki sportowe

You write awsome article, bookmarked

Fajerwerki

interesting blog, bookmarked for the future referrence, what template do you use ?

Re: Fajerwerki

Thank you!
Did you mean site template? If so I'm using my own theme and you can get it here.

Regards!
Shahnawaz

pudzianowski najman zaklady

Your blog is full of interesting articles, thanks for good read

...

I'm always looking for recent blogposts in the internet about this topic. Thanx.

FOREX currency trading

Excellent article, i just want to thank you for good read..

What anonymous function are you using?

I really like your script but I have one small problem.
I can't seem to find the function you are using to generate the callback=? parameter.

I'm using Anonymous function

I'm using Anonymous function, a function which has no name but the calling script will get it automatically.

Actually this is done by jQuery's $.getJSON function.

Hope this works!

Regards!
Shahnawaz

My Favourite Website

Great site, how do I subscribe?

you can subscribe

Thanks!

You can subscribe by clicking RSS Feed link on the left pane in Stay Tuned block.

Hope this works!

Regards!
Shahnawaz

Issue with code

If i just straight up copy and paste your code in, everything works. But if I put in my testing url where i have 10 test post i get nothing..just says loading the whole time. Do you have any other settings set up inside of blogger that I'm not seeing?????

tatoo designs

Your blog is awsome, bookmarked, regards tatoorexion
tattoo designs

OK found it

ummm summery was throwing an error I don't have a summery in my basic blog

question

Hey thanks for your work

I am not as lucky as I thought that I might be following your example. Funny thing is that it works when I use your blog.
http://stevenjhagen.com/testing02/teststeven03.html

But mine not so much
http://stevenjhagen.com/testing02/teststeven04.html

Might I have a security problem or a api issue?

if you got time to help me that would be really cool. I will now reread your article to see if I missed something
thanks
Steve

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <img> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options