This script creates a text link to a random post on your Blogger blog. It displays the random post's title as well as a brief random message to accompany it.
This is an updated version that replaces my former version of the code. The major changes between this updated version and the current version are:
- The addon no longer requires the Archive Widget to be added to your page.
- The code is no longer limited to displaying posts from the past three months. It can now give you links to posts from your blog's entire history.
How it Works
For those of you who are technically inclined, you may be interested in how the code works. The key operation of the random post link script is fundamentally tied to JSON (JavaScript Object Notation).
Fortunately, Blogger is JSON enabled, meaning that it can generate Javascript in an object format that allows it to pass Javascript widgets a list of the post titles and links on the blog.
After the hard part of receiving and parsing the links is over with all it takes is a simple random number function:
A random post title is selected, and printed using the document.getElementById().innerHTML functionality.
function random_number(range)
{
return (Math.round(Math.random()*200000)%range);
}
Adding this Feature to your Blogger Blog
- Enter Layout Mode
- Find the area where you want the link to appear and click Add a Gadget.
- Select the HTML/Javascript gadget.
- Paste the following code into the gadget:
- <div id="output_div" style="font-size: 11pt;">
</div><br/>
<script>
/* <!-- <[CDATA[ */
var introArray = new Array(
"Have you read ^?",
"Check out ^?",
"Check out a post from the archives: ^",
"I picked out an old post just for you: ^",
"Would you like to read ^?",
"A previous post: ^?",
"You might enjoy reading ^.",
"Check out this post from the past ^."
)
function random_number(range)
{
return (Math.round(Math.random()*200000)%range);
}
function random_post(json)
{
var number_of_posts = json.feed.entry.length;
var random_range = number_of_posts;
var selected_post_number = random_number(random_range);
var post_title = json.feed.entry[selected_post_number].title.$t;
var post_link = ""
for (var k = 0; k < json.feed.entry[selected_post_number].link.length; k++)
{
if (json.feed.entry[selected_post_number].link[k].rel == 'alternate')
{
post_link = json.feed.entry[selected_post_number].link[k].href;
break;
}
}
var saved_link_html = "<a href='"+post_link+"'>"+post_title+"</a>";
var output_string = introArray[random_number(introArray.length)];
output_string = output_string.replace("^",saved_link_html);
document.getElementById("output_div").innerHTML=output_string;
}
/* ]]> --> */
</script> - Now save the layout and switch to the tab Edit HTML.
- Scroll down to the very end of the XML template and find the </body>
- Just above that statement paste the following code:
<script src="/feeds/posts/summary?alt=json-in-script&callback=random_post&max-results=99999" type="text/javascript"></script>
- Save the template and view your blog. The random link should appear where you put the HTML/Javascript Gadget.
You may notice that about one out of every ten random links is not to a blog post but back to Experiment Garden. I would appreciate it if you left this, as it shows appreciation for the time and effort that I went to to create this script. However, you may not like the effect. There are two options you might want to take.
First, you can make the advertisement appear less often by simply adding more of your own custom messages to the list of random messages.
Find the section of code:
var introArray = new Array(
"Have you read ^?",
"Check out ^.",
"Look at ^.",
"Just for you: ^",
"Please look at ^.",
"Would you like to read ^?",
"You might enjoy reading ^.",
"Do you want to read ^?",
"Please read ^.",
"Random link powered by <a href='http://experimentgarden.blogspot.com/'>Experiment Garden</a>"
)
To add another message just copy one of the lines in the middle, and paste it again in the middle. If you want to customize the message, just keep in mine that the caret "^" is where the random link is inserted.
If you want to remove the link back to Experiment Garden completely, just remove the last message in the list. After you remove this message, be sure to remove the comma at the end of the new last message. If you remove the link back to Experiment Garden then please add another link somewhere. Perhaps add me to your blogroll, or else write a post about the addon with a link to this page.
Limiting Number of Posts Used
Another customization that you may want to do is limiting the number of posts that the script can choose from. As it is the script will display links to any of your posts up to the 99999th post. You may want to limit it to the last 100-200 posts for several reasons. For one thing your oldest posts might be out of date, or their writing quality might not be as good as your new posts. But another reason why you might want to reduce the number of posts is load time. Loading a list of all the posts you have ever written on the blog can be time intensive for older blogs. So to reduce the number of posts that the script will choose from find the number 99999 in the code that you added to the XML template and change it to a smaller number. For example, the number 100 will limit the results to the last 100 posts that you have written.
I sincerely hope that this Blogger addon can be of help to you. If you encounter any issues please let me know by commenting here, and I will do my best to get back to you as soon as possible.
24 comments so far. What are your thoughts?
Hello I gave this a try cheers but the html would not parse it's the seperate bit you add in the body
It kept complaining about the callback feature needing the ; but no matter what edits I tried it would not take it at all.
cheers for the idea - Glen.
I apologize, that error was caused by the source code format tool that I was using. It inserted a semicolon in a place where there was not supposed to be one.
It should work now.
I get this error:
Your template could not be parsed as it is not well-formed. Please make sure all XML elements are closed properly.
XML error message: The reference to entity "callback" must end with the ';' delimiter.
Okay, I fixed that problem.
Sorry, folks.
I tried to add fancy source code highlighting to the code, but it just messed up the important character formatting needed for Blogger to use the code in the template.
I made the corrections. Please try them out to see if they work.
I'm sorry, but I just can't this to work.
Which blog are you trying to install it on?
Also, were there any specific error messages?
This addon works fine on other blogs its been installed on, so I'll have to look at your site to see what is wrong.
Thank you.
No, this time there's no error message. It just doesn't seem to show up.
My blog is http://beyondmyslab.blogspot.com/
I have the short snippet of code inserted automatically at the bottom of each post. On the blog itself, I can see a blank space beneath each post text, but no link shows up. Is the short snippet of code supposed to be an Element all its own somewhere?
I can't think of what I'm doing wrong.
Okay, I think I've got it now. What did I do differently? I really don't know!
I was actually hoping for something that would show at the end of each post rather than across the top of the blog. But your hack is nifty. Thank you.
Your problem is that you had multipe copies of the JSON code. This code hack is only designed to create one, single random post link.
If you want more, beneath each post I'll have to modify the code to work a different way.
Nathan,
I was hoping to end each post with a link to a random post. I have over a 1,000 posts now, and it seems a shame not to give the archives an airing in some way, even if they are me mostly complaining about not being able to find a marketing job.
There's a blog I read that ends each post with a link to a post from 2008 and then 2007. The blogger sets up the links manually. I like the idea but it's tedious to search for the old posts, get the addresses, and set up the links manually, so I was hoping for a random post link after each post.
I do appreciate you posting your code hack. It's very kind of you.
Let me think about this. I'm trying to come up with an elegant way to put a random post link at the end of each post, but I can't think of any way of doing it that wouldn't be awkward.
Did you want the random links to appear only on post pages, or on the main page as well, beneath each post?
I don't want to put you to any trouble, Nathan.
I picture the link appearing at the end of each post on the main page only.
Thanks for linking to me. I really do quite love this addon. <3
@Ari: I'll put it on my to-do list, and see what I can do.
@Jessi: You are welcome! You have a good blog with the code installed correctly, so it is my pleasure to have it showcased there.
Thanks for linking to me as well. :)
I put a link on my sidebar to you, btw
I'm experimenting with changing the random number range code because the add-on is only pulling posts from now back to May 2008. It's not pulling posts from 2006 through April 2008.
I added some shading to make it look better on my blog.
I'm having fun playing with this. Thanks for posting this add-on, Nathan!
http://www.beyondmyslab.blogspot.com
Unfortunately after experimenting with the code myself it appears that the addon will only pull out the last 500 or so posts.
Theoretically, the post request code requests the last 99999 post links, but Blogger will only give it the last 500 post links. I think this is because Blogger doesn't want to overload their servers.
Maybe self-hosted blogs can get around this limitation, but it's definitely impossible for subdomain Blogger blogs.
To change the styling of the link, modify or add CSS style attributes to the place where it says " style='font-size:11pt;' "
Thanks for the blogroll link Ari. I appreciate it. I actually programmed this addon for my own blog, but I'm glad that it has been able to be of so much use to other people as well.
Thanks Nathan - works a treat now - added it in without removing your random promo and also added you in on blogroll.
great stuff
http://www.glens-life.blogspot.com/
Cool!
Thank you very much Glen. I'm glad it can be of help to you.
Thanks for the blogroll link!
Veru useful! Thank you!
I'm using the older version of this widget and I noticed the code is not limited to displaying posts from the past three months, as you said in this update. It shows links to posts from my blog's entire history. I hope is not just on my Pc, though...
Anyways, nice widget. Come check it out on my website as well:)
Okay, you must be using an older version that still operates by JSON. The really old version that required a blog archive was limited to displaying a certain number of posts that were available in the HTML of the blog archive.
Very nice blog, and you application of the random post widget looks very nice.
Thanks for stopping by.
I'm back, with another question this time, and hope I won't become a pitty in the future:) :
Is it possible to add idependent views for each post? This should be measured bi clicks (I think) and I'm not talking about site counters and other stuff of page counters. I saw today on a site, they have on each post a view number of that post, near the post name. I was thinking if that would be possible with blogging platforms, such as blogger.
I can send you individually the website, to see for yourself what I'm taking about, if it's the case.
Leo
Example:
XX New Album
Comments (20) 6,177 views | September 22nd, 2007 by Admin
Posted in: HipHop Headlines - News
That would definitely be possible on WordPress. However, on Blogger it would be very difficult if not impossible. This is why:
To keep track of how many post views you need some way of storing a value of post views. Blogger doesn't give you a place for storage on their server, and can't handle the more advanced programming languages you would need to store data on the server.
On a self-hosted Wordpress blog you can use PHP and a database or even a plaintext file to save the number of times that the post has been viewed, but on Blogger you would have no place and no way to store that page view value.
very nice widget dude!!
then i want to show some more number of posts in random,please help!!!
Is it possible to add the date of the post to the title? Also, if possible, it would be nice to display the first couple of lines from the post to give the reader a quick preview of the post before they click it. Otherwise, it's a great tip!