Javascript Blogroll Randomizer

Eric uses Blogger and wanted to have a random list of 10 blogs from his blogroll display each time someone visited his blog. Since Blogger doesn't let its members upload PHP files, I used the following Javascript to create a random blog listing for him. The code is a slightly modified version of what I found here.

8/28/2007 Update: Be sure to include the backslashes before single quotes and apostrophes. Chicken Girl wrote a great explanation of why this is important at Cranky Fitness.

<script type="text/javascript" charset="utf-8">

/* list all blogs, put a \ before single quotes */
var chess_blogs = [
'<a href="http://www.beginchess.com/">Begin Chess</a>',
'<a href="http://caughtinthefire.blogspot.com/">Caught in the fire</a>',
'<a href="http://chessloser.wordpress.com/">Chessloser</a>',
'<a href="http://chess-training.blogspot.com/">Chess Training</a>',
'<a href="http://dk-transformation.blogspot.com/">DK Transformation</a>',
'<a href="http://dutchdefence.wordpress.com/">Edwin \'dutchdefense\' Meyer</a>',
'<a href="http://grandpatzerchess.blogspot.com/">Grandpatzer Chess</a>',
'<a href="http://www.kenilworthchessclub.org/kenilworthian/">Kenilworthian</a>',
'<a href="http://likesforests.blogspot.com/">Likeforests</a>',
'<a href="http://thebackrank.blogspot.com/">Loomis</a>',
'<a href="http://wangschesshouse.wordpress.com/">Patzer\'s Corner</a>',
'<a href="http://knightskewer.blogspot.com/">Pawn Shaman</a>',
'<a href="http://phorku.blogspot.com/">Phorku</a>',
'<a href="http://qxh7.blogspot.com/">Qxh7#</a>',
'<a href="http://rlpchessblog.blogspot.com/">Robert Pearson\'s Chess Blog</a>',
'<a href="http://sonofpearl.blogspot.com/">Son of Pearl</a>',
'<a href="http://squirrelchess.com/">Squirrelchess</a>',
'<a href="http://www.eddins.net/steve/chess/">Steve Learns Chess</a>'
];

function randomize (a_items, n_count) {
    var n_index, s_html = '<ul>';
    while (a_items.length && n_count) {
        n_index = Math.ceil(Math.random() * a_items.length) - 1;
        s_html += '<li>' + a_items[n_index] + '</li>';
        a_items[n_index] = a_items[a_items.length - 1];
        a_items.length = a_items.length - 1;
        n_count--;
    }
    return s_html + '</ul>';
}

// call randomizer
// param 1 - list of items in the form of a variable
// param 2 - number of items to display
document.write(randomize (chess_blogs, 10));

</script>

Comments

Submitted by Anonymous on Sun, 08/19/2007 - 02:41

Very smart!

How lucky BDK is to be with a woman of your consumate calibre.

I'd be fortunate to know a woman with half your skills!

warmly, david k, seattle

Submitted by Anonymous on Sat, 08/25/2007 - 22:49

So this sounds like a great idea but I couldn't get it to work on my blog. However, I'm pretty clueless with this stuff, so I suspect I did something wrong. Nothing on the list would display.

I basically copied your text into notepad, put in my own blog roll list in instead of the chess one, (formatted the same way) and changed chess_blogs to friendly_blogs where it appeared (twice). Then pasted the whole thing into the html/java 'add elements' thingy on the blogger layout page. I have the new blogger, not the old one.

Are there other variables that a smart person knows need to replaced in there, or other stuff a clueless person would need to do to make this work?

I was hoping someone had come up with a something like this, and was very pleased to see you have a working version. Just don't seem to be technical enough to get it to work for me.

I know you're probably very busy, but if you have any suggestions, they'd be very welcome. Thanks!

Crabby McSlacker

http://crankyfitness.blogspot.com/

Submitted by Anonymous on Sat, 08/25/2007 - 23:21

I think the problem may have been an errant apostrophe in one of the blog's names. Have only played with it on my test blog but am looking forward to putting it on my real blog to help reign in the ever-expanding blogroll. Thanks so much for this!

Crabby McSlacker

Submitted by BD Knight on Mon, 06/15/2009 - 19:32

This seems to not work in IE8 (look at the chessconfessions site in firefox it works, and IE 8 nothing shows).