Blogger Feeling Lucky Widget (Random Post)

Comments and Problems can be reported at the home post for this at Blogger Feeling Lucky Widget

Creates a link to a random post on your New Blogger blog.  Feeds must be enabled.

One-click add to Layouts templates


Input the anchor text you want. You can input an image, but use a complete img tag. Then click the add to blogger button to add to your layout. You can change the title, or make it blank if you don't need one. The widget can be placed anywhere a widget will go.





For classic template on blogspot , or in a layout template if you want more control over where the link is inserted you can use the following. Paste the below script code in the <head> of the template

<script type='text/javascript'>
//<![CDATA[
function showLucky(root){
    var feed = root.feed;
    var entries = feed.entry || [];
    var entry = feed.entry[0];
      for (var j = 0; j < entry.link.length; ++j) {
       if (entry.link[j].rel == "alternate") {
       window.location = entry.link[j].href;
       }
      }
   }

function fetchLuck(luck){
    script = document.createElement('script');
    script.src = '/feeds/posts/summary?start-index='+luck+'&max-results=1&alt=json-in-script&callback=showLucky';
    script.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(script);
   }
function readLucky(root){
    var feed = root.feed;
    var total = parseInt(feed.openSearch$totalResults.$t,10);
    var luckyNumber = Math.floor(Math.random()*total);
    luckyNumber++;
    fetchLuck(luckyNumber);
    }
function feelingLucky(){
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = '/feeds/posts/summary?max-results=0&alt=json-in-script&callback=readLucky';
    document.getElementsByTagName('head')[0].appendChild(script);
    }
//]]>
</script>

Then put the following link wherever you want

<a href="#random" onclick="feelingLucky()" title="Random Post">Feeling Lucky?</a>

You can change around the anchor text to suit.


FTP blogs, can still use it, but need the following modified script instead of the above
<script type='text/javascript'>
//<![CDATA[
var luckyBase = 'http://www.blogger.com/feeds/YOURBLOGID/posts/summary';
function showLucky(root){
    var feed = root.feed;
    var entries = feed.entry || [];
    var entry = feed.entry[0];
      for (var j = 0; j < entry.link.length; ++j) {
       if (entry.link[j].rel == "alternate") {
       window.location = entry.link[j].href;
       }
      }
   }

function fetchLuck(luck){
    script = document.createElement('script');
    script.src = luckyBase + '?start-index='+luck+'&max-results=1&alt=json-in-script&callback=showLucky';
    script.type = 'text/javascript';
    document.getElementsByTagName('head')[0].appendChild(script);
   }
function readLucky(root){
    var feed = root.feed;
    var total = parseInt(feed.openSearch$totalResults.$t,10);
    var luckyNumber = Math.floor(Math.random()*total);
    luckyNumber++;
    fetchLuck(luckyNumber);
    }
function feelingLucky(){
    var script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = luckyBase + '?max-results=0&alt=json-in-script&callback=readLucky';
    document.getElementsByTagName('head')[0].appendChild(script);
    }
//]]>
</script>
The line
var luckyBase = 'http://www.blogger.com/feeds/YOURBLOGID/posts/summary';
MUST be modified to use your blogs unique id number.  You can either get this from your dashboard links (links will all have blogID="XXXXXX", with the XXXX's being your ID) or it can be viewed on the source of your published page in the meta information. Use the same link as above to activate the random post.