Create A Random Post Link In Blogger

Create A Random Post Link In Blogger
This is how you can create a link that will randomly load a post page from your blog archive upon clicking the link. This will involve of creating a javascript widget first. 

A link will be created seperately that activates the javascript to randomly load a post page from the blog archive.


Creating the Javascript
Step 1: In your blogger, goto Design > Page Elements.
Step 2: Add a gadget.
Step 3: Select HTML/Javascript.
Step 4: Copy and paste the code below to the gadget content.

-------------------------------------------
<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>


 -------------------------------------------
Step 5: Save the gadget without a Title name for the gadget.

The Link Code

Step 6: Use the link code in your blog as a widget/gadget or insert it into your blog template. That's depends on how you want it to be.

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

References/Credits:
http://almost-a-technocrat.blogspot.co.id/2010/08/create-random-post-link-in-blogger.html

Post a Comment

Previous Post Next Post