It is made up of two main parts. First, place the following code where ever you want the random message to appear:
Now place this code section below both the blog archive and the section that you just placed. The best place is probably the bottom of the page. The script will retrieve a random post from your blog archive and display a message advertising it.
/*<[CDATA[ <!-- */
//Beginning dynamic archive extract code.
//Locate the archive list.
var archive = document.getElementById("ArchiveList");
var archive_html = archive.innerHTML;
var links = ;
var introArray = new Array(
"Have you read ^?",
"Check out ^.",
"Looking for something to read? How about ^.",
"My blog says: \"Look at ^\"",
"I dredged up an old post just for you: ^",
"Please look at ^.",
"I wonder if you'd like to read ^?",
"You might enjoy reading ^.",
"Look what I found for you: ^",
"My blog has a post just for you: ^."
//Now search for child elements that have the class "posts"
//These contain the links that we want to extract.
var found_place = archive_html.search('<');
var extract_string = archive_html.substring(found_place,found_place+10);
//Check to see if we found the beginning of the link section we want.
//Found the link list, now go through and extract the links.
var inner_search = archive_html.search("<");
var loop_flag = true;
//Finished with this archive section so exit.
loop_flag = false;
//Found a link to extract, now find the end of the link.
var end_link = archive_html.search("</a>")+4;
if(end_link==3) //Returns negative one if fail, so +4 becomes 3.
//Must be Internet Exploder, do a capital search.
end_link = archive_html.search("</A>")+4;
//Do the slice
links[links.length] = archive_html.substring(inner_search,end_link);
//Cut out opening just found.
archive_html = archive_html.substring(inner_search+1);
inner_search = archive_html.search('<');
//Trim out the opening we just found.
archive_html = archive_html.substring(found_place+1);
found_place = archive_html.search('<');
//Great, now we have a list of links from the archive
//Do something with them.
var output_string = introArray[random_number(introArray.length)];
output_string = output_string.replace("^",links[random_number(links.length)]);
//Pick a random link and put it in the output area.
document.getElementById("extract_output").innerHTML = output_string;
/* ]]> --> */
Further notes: Your blog archive widget must be organized by months so that the code can pull out the links.
Hopefully this can be of some help to you as a blogger. If you like this script and decide to use it on your blog please link back to my blog or to this post specifically. I thank you in advance.