Oct 22, 2009

Count your blog posts!!

I've heard a few people ask about ways to count the number of posts we've submitted so far on our class blog. A few of the answers I heard were good suggestions, but didn't seem to produce results to meet my standards. I think that going through and counting every post manually is annoying and tedious, and as the class progresses, we will only accumulate more and more posts, making it all the more tedious and time consuming. My immediate reaction to all this was "Hey, why don't I build something that will count the posts for me!?" The more I thought about it, the more I realized this task of creating such an application wouldn't be all that hard to create.

After Professor Drake's example a week or two ago where he used the blogger API to pull back a few of the most recent posts on our blog (25 if I remember correctly), the idea of creating this dynamic counting app became even more feasible to me. I haven't had a lot of time between, our individual and group projects, plus my other classes and my job, but I found a couple hours in my day today to put together this idea I've been brainstorming for a few days now and I thought you guys would be interested in how it works.

The basic idea of this project is to contact the blogger API asynchronously using AJAX (just like any other AJAX call to other APIs) and then request our blog response from it. By implementing the code below that Professor Drake showed us for cross-server communication, all I had to do was find a way to display the XML response from blogger in a way that makes sense to our small human brains.



After the usual AJAX call, instead of displaying the normal "request.responseText," I needed to format it by sending it to a new function and figuring out the XML. Since this is XML though, an important difference here that we talked about in class briefly, is that I must change the "request.responseText" to "request.responseXML."



As Professor Drake explained, parsing through the XML file manually, as he did, is somewhat difficult to do, and requires that you know exactly where each element is within the XML in order to get the contents of it. I found a way to use an array to store the author names of each post found on our blog, then later I manipulate the array to count the names. My first task was to figure out how to get the author names from the XML data. Similar to getElementById, JavaScript offers a function called getElementsByTagName. The plural "Elements" tripped me up at first, as I'm used to it being singular in getElementById. Anyway, with the code below, the variable I created called "author" becomes populated with all the authors' information found within the XML.



Now that the authors' information is in one variable, I can further parse through it in a for loop in order to pull the tag from each of the tags and get the text from that to find out the names. This is where I place the names into an array, allowing me to then edit the array after the loop is done.



Now my array contains all the author names found in the XML file, including the duplicates of each of our names if we have posted more than once. By creating another for loop, I can compare the name entered in the text box on the home page with each element in the array and every time it matches, a counter is increased by one.



The only thing left to do after the counter has counted all the matching occurrences of the name entered, is to return that number (and in my case a little friendly text to go with it) back to the original function which is now ready to output it on the screen via the innerHTML call I made on it.



There ya have it! Feel free to check it out and use it to keep track of your posts, I may update it later so that you can select your name from a select box like Professor Drake did, rather than having to type it in each time and worry about typos, but we'll see if I find time for that. In the mean time it works, and it should solve the problem of counting all your posts by hand!

It's on my site, right here: http://people.emich.edu/mmager/449/


EDIT: I have fixed the issue with IE, feel free to use this app on any browser now! :)

12 comments:

  1. Matt,
    Excellent post, I put my name in and for some reason kept getting the dreaded cannot find name. I may be putting the wrong Author in an will continue to see if I can reslove

    ReplyDelete
  2. Matt, I found the same problem as Joushua and I found that it works with Firefox and not IE so, something to look at in your spare time. Very Nice.

    ReplyDelete
  3. Hey Matt,
    Thanks for the post and of course for the blog counter. I got my count right away. It was correct too because I actually just counted them. Joshua and Terry, I got it to work when I typed in your name just how it shows on your comment. I do use firefox though.

    ReplyDelete
  4. Matt, your post is awesome! Thanks for making our lives a little bit easier with all of your hard work. I too tried this from home but was unable to get it to work but in a few days I'll be back on campus and will try it again. Meanwhile, I'll just continue blogging and hopefully get ahead (just a personal goal of my own).

    ReplyDelete
  5. That is it, I use IE by default. I tried it in Firefox and found that I need to get crackin' on more posts. Thanks for the utility

    ReplyDelete
  6. Whoops, sorry guys and thank you for the IE note. It appears that my IE catching isn't working correctly, so I'll have to look at that...In the meantime, use Firefox! Firefox is faster and better than IE anyway!! Especially when developing...as you can see here. Just a little promo for Firefox instead of the always inferior IE!

    ReplyDelete
  7. Ok everyone, I fixed the IE problem. Apparently IE doesn't understand "textContent" but instead uses just "text". Well Firefox (and others) use "textContent" but don't understand just "text," so I had to account for which browser was being used upon making the request. It should work fine now regardless of browser choice. I still recommend Firefox though! :)

    ReplyDelete
  8. Matt,
    Thanks for getting the IE part of it to work, I know it pained you greatly :D

    ReplyDelete
  9. Matt, thank you for this post. This makes it easier to track the number of posts and reminds someone....
    Thank you

    ReplyDelete
  10. Matt,

    As others have already stated thank you creating this, definitely makes it easier to keep track of ones progress.

    ReplyDelete
  11. To jump on the band wagon, thank you for your site. I saw that you have updated it, and noticed that there is a problem. I do not think that the comments are counted correctly. You may want to take a look at that so people do not go crazy
    :)

    ReplyDelete
  12. This is such a great idea. Making use of the blog API and turning it into useful information is exactly what we are trying to accomplish in this course. Thank you for helping all of us keep track of our blogs.

    ReplyDelete