Ask your JavaScript questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to count the number of items in this RSS feed? JavaScript


Go to the front page of this site. Look either up at the top or in the right-hand sidebar. You will see some html like this:

<p>Our other sites:</p>
<ul id="our_other_sites">
<li id="jq_url_and_total_active_count"><a class="tma_site_url_to_count_active" href="">Javascript Questions</a> <span id="javascript_questions_active"></span></li>
<li id="mysql_url_and_total_active_count"><a class="tma_site_url_to_count_active" href="">MySql Emergency</a><span id="mysql_emergency_active"></span</li>
<li id="tr_url_and_total_active_count"><a class="tma_site_url_to_count_active" href="">Tutor for Ruby</a><span id="tutor_ruby_active"></span</li>
<li id="se_url_and_total_active_count"><a class="tma_site_url_to_count_active" href="">Symfony Experts</a><span id="symfony_experts_active"></span</li>

What I want is to see how many questions are active on each site, like this:

Our other sites:

WP Questions (9)
Javascript Questions (1)
MySql Emergency (0)
Tutor for Ruby (0)
Symfony Experts (1)

You will note that every site has a feed that shows the active questions:

I need some Javascript, possibly jQuery, that upon the page loading, will look up each feed, count how many items are present, and put the number in the span tag.

The true winner would be anyone who can do this in a general purpose way using the CSS class selector. However, if instead people only offer hard-coded, per site versions of this code, I will live with that.

Answers (5)


Ross Wilson answers:

We can do it with javascript, but it might be something better suited for a backend script because of the rules around cross-domain resource sharing. I have started a little example, but if it isn't run from the same domain we get back an error saying that content cannot be loaded from another domain. You would first need to enable CORS for your feeds: [[LINK href=""]][[/LINK]]

Lawrence Krubner comments:

Huh, that leaves me curious how we did this back when we had the blog. I suppose I could dig up the old code, but I have no time right now, which is why I'm trying to enlist the help of people here.

You are saying this should be done with something like fsockopen which should read each RSS feed?

Javascript would be ideal since the front pages are cached and only updated every 5 minutes. Not that a 5 minute delay is terrible.

I am curious, could this work from a central domain? All of the sites include Javascript from the domain

I had not thought about the fact that this would be difficult.

Suppose we limit this to the newest browsers and to HTML5? WebSockets can do cross-domain calls, so suppose we go that route?

Ross Wilson comments:

Maybe you were using jsonp before to do the cross-domain requests? Is there a way to get the feed back as json data?

Websockets could be used, but may leave out a portion of your userbase. I think using a script like Rainner posted, with some caching might be your best bet.

If you run this code while on the same domain you can get the result you are looking for, but if you run it from another domain you will run into issues:

console.log($(d).find('entry').length || 0);

Lawrence Krubner comments:

Yes, I could offer the same feed as jsonp. That would be trivially easy.

Ross Wilson comments:

That would probably be the best cross-browser way of doing it then. When that is ready I can give you a general purpose function that could be used for all the sites.


Rainner Lins answers:

As mentioned, javascript alone is not allowed to go around grabbing remote data due to security issues. However, this can be done using your own server script to do the fetchig for you, then using javascript to call your own script and update the feeds.

I wrote an example for you. You can view the JS source code form the browser, and here's the PHP script that fetches the RSS feeds and does the counting...

Please have a look at the script hosted on my site here:

[[LINK href=" "]] [[/LINK]]

And the PHP source:

// runtime
error_reporting( 0 );
ini_set( 'display_errors', 0 );
set_time_limit( 10 );

// fetch content from a remote url
function fetch( $url='' )
$url = trim( html_entity_decode( urldecode( $url ) ) );
if( empty( $url ) ){ return false; }
$d = false;
$c = curl_init();
curl_setopt( $c, CURLOPT_URL, $url );
curl_setopt( $c, CURLOPT_CONNECTTIMEOUT, 6 );
curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
$e = curl_error( $c );
$d = curl_exec( $c );
curl_close( $c );
if( !empty( $e ) || empty( $d ) ){ return false; }
return $d;

// get the remote XML and parse it
if( !empty( $_REQUEST['feed_url'] ) )
$data = fetch( trim( $_REQUEST['feed_url'] ) );
$data = ( !empty( $data ) ) ? preg_replace( '/&[^; ]{0,6}.?/e', "((substr('\\0',-1) == ';') ? '\\0' : '&amp;'.substr('\\0',1))", $data ) : $data;
$data = ( !empty( $data ) ) ? simplexml_load_string( trim( $data ), 'SimpleXMLElement', LIBXML_NOCDATA ) : $data;
$total = ( !empty( $data->entry ) ) ? count( $data->entry ) : 0;
die( json_encode( array( 'total'=> $total ) ) );


Martin Pham answers:

hi Lawrence,
Please try this

jQuery(document).ready(function($) {
$('.tma_site_url_to_count_active').each(function() {
var s = $(this),h = s.attr('href'),pr = s.parent();
if(h.substring(h.length-1, h.length) != '/') {h += '/';}
h += 'feed';
var qr = ""+encodeURIComponent(h);
$.getJSON(qr + "&output=json_xml", function(j) {
if (200 == j.responseStatus) {
var f = j.responseData;
if (j = f.feed) {

for example: [[LINK href=""]][[/LINK]]

[[LINK href=""]]phim[[/LINK]]

Lawrence Krubner comments:

Very clever. How does it work?


LatinUnit answers:

How about doing a MySQL query, counting the number of actives found in the questions table such as

SELECT COUNT( question_status) AS active_questions
FROM questions_table
WHERE question_status = "Active"

Description: question_status is the field/column that holds whether the question is active/answered or in voting etc etc
the where clause will filter the table and only count how many active questions it found.
That would work good. I just tested it and it work, you try it

Lawrence Krubner comments:

No, the sites should live without knowledge of each other's databases. (We built the software with the thought we would license it to others, so every site gets its own database.) This needs to be done via Javascript and RSS. I should add, we had this working for 2 years, back when we had a central blog at I'm trying to resurrect that, but bring this onto each site.

LatinUnit comments:

Hi, the query will count from the database is currently connected to as defined in the settings, so the site will not have knowledge of other sites.

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
die('Could not connect: ' . mysql_error());

// some code

LatinUnit comments:

One question, in which year did you have the website working with this functionality, was the code stored in a js file?

Lawrence Krubner comments:

<blockquote>the query will count from the database is currently connected to as defined in the settings, so the site will not have knowledge of other sites</blockquote>

Yes, but if we are on Javascript Questions and we want to know how many items are active on WP Questions, then Javascript Questions would need access to the WP Questions database. I would rather avoid that if possible.

I am not sure how we did it before, when we had it working. Maybe it was all PHP.


Jarret Minkler answers:

If you have all the sites on the same hosting account you should just be able to query the databases one by one.

Bonus points if you can setup on your hosting and create stored a stored procedure.

With MySQL Proxy you can have the same dns string set in each app but it can route the queries to the correct databases.

Another route would be to hit a counter API. Setup a url to hit when someone posts a new question or one becomes inactive. Then it can simply return a counter value for each site at a different endpoint. No expensive database or xml traversal required.

Or again if they are all on the same shared account, you can write to a local file with the counts etc.

Lawrence Krubner comments:

I am sorry if I was unclear, but querying the database is not an option. That would result in a high level of coupling between sites that need to remain independent. Sites should talk to each other through their APIs.