Blogspot hack: random quotes in sub-header

I wanted to put random quotes instead of the same old blog description. Here's how I did it.

Classic Blogger

Find the $BlogDescription$ tag in your template and replace with the sample code below.

Blogger Beta

While in Edit HTML, click on the Expand Widget Templates checkbox, then look for <data:description/> where you will replace this tag by the sample code below. Don't forget to replace $BlogDescription$ by <data:description/>.

Sample code


<p id="description">
<script language="javascript">
var asText=new Array(
'quote1:simple text',
'quote2:<i>embeded HTML</i>',
'quote3:<A href="http://somelink.com/"
target="_new">with hyperlink</A>'
);
document.write(
asText[
Math.round(Math.random()*(asText.length-1))
]
);
</script>
<noscript><$BlogDescription$></noscript></p>

How does it work?

  • We want to keep the same formating for the sub-header (the blog description), so we keep the <p id="description"> tag.
  • the JavaScript asText array can be as short or contain as many entries as you want, as long as you take care of enclosing each one in single quotes, and separate them with commas (excet the last one). You can embed HTML syntax within the text to alter its display (quote2), and even links (quote3).
  • Once the array is defined, the last thing to do is simply to write one of the quotes randomly. Lets look at this closer:
    1. Math.random() generates a random number between 0 and 1.
    2. When multipled by the number of entries in our array (asText.length-1), we get a random number between 0 and the number of entries in the array (we substract 1 because the first position of an array, in JavaScript, is 0, not 1).
    3. asText[n] provides the text of the element n of the array
    4. Finally, document.write is the JavaScript function used to output the text.
  • We keep the original $BlogDescription$ within <noscript> so crawlers (like Google), will see your original text and take it into account in search results (you could also remove the <noscript> tags if you still want to show your original Blogger description).
See my other Blogger hacks:

10 comments:

Carlitos! said...

Sorry... i know that this post is really old, but i wonder..

1 - where do i write, in this code, the new description?
2 - if i want more than 2 descriptions, where should i write the other ones?

Thank you

S.Hamel said...

Simply replace where it says 'quote1', 'quote2', 'quote3' with your own text. You can have as many as you want as long as each one is enclosed in quotes and separated by a comma (this is a JavaScript array of strings).

gowang40 said...
This post has been removed by the author.
gowang40 said...

S. Hamel,

Thank you for writing this random quotes instruction for people like me with no idea about javascript. I can get my blog to display 3 random quotes in text now, however, I have problem with quote 3 type hyperlink: What I did is to replace where the link between " " to my link and where it says >with hyperlink< to the text I want to display, but the result is a blank display. Can you help? Thanks again.

Erik said...

I can see it working on your page but can't seem to get it functioning on mine.
I think this is mostly because I cannot find "$BlogDescription$" anywhere, much less near "data:description/". I tied a few different things, only to make my blog description disappear entirely. any guesses?

Caroline said...

gowang,

I had the same problem. I fixed it by using double quotes around the strings in the array instead of single quotes:

"quote3: <[a href]='http://somelink.com'>your quote<[/a]>"

...without the [], of course.

I'm not sure why that would work - I don't know Javascript too well either - but it did for my page. (By the way, when you do this, you also have to switch the double quotes inside the string to single quotes, as the example shows.)

Karl L. Gechlik said...

We just got it working - see it in action here

AskTheAdmin.com

I love it so much! I have to come up with more quotes!

Ike said...

Is there a way to put this into a sidebar widget?
Thanks, Ike
http://ikes-adventures.blogspot.com/

S.Hamel said...

@ike: sure, just add a HTML/JavaScript Page Element and put the code in there.

Kelly said...

I think this doesn't work as described anymore. I posted what worked for me here. If I did something wrong there, I'd love to know about it!

Thanks for the great hint.