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="">Begin Chess</a>',
'<a href="">Caught in the fire</a>',
'<a href="">Chessloser</a>',
'<a href="">Chess Training</a>',
'<a href="">DK Transformation</a>',
'<a href="">Edwin \'dutchdefense\' Meyer</a>',
'<a href="">Grandpatzer Chess</a>',
'<a href="">Kenilworthian</a>',
'<a href="">Likeforests</a>',
'<a href="">Loomis</a>',
'<a href="">Patzer\'s Corner</a>',
'<a href="">Pawn Shaman</a>',
'<a href="">Phorku</a>',
'<a href="">Qxh7#</a>',
'<a href="">Robert Pearson\'s Chess Blog</a>',
'<a href="">Son of Pearl</a>',
'<a href="">Squirrelchess</a>',
'<a href="">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;
    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));



Awesome!!!! Thanks!


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


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

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

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