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:

  1. Blogger Feeling Lucky Widget (Random Post)

Comments

  1. Thank you so much! I've been looking for this for ages!

    ReplyDelete
  2. can i maybe put a picture under the text? y'know, just for decorations.

    ReplyDelete
  3. Hi there,
    I'm having a problem with this.. Installed it the first time and it worked great (thank you!) but had to reinstall later on and now the button won't work. What could be the problem?

    ReplyDelete
    Replies
    1. Hi Sam, sorry for the late reply. Unfortunately I have not been using this code for some time after I took down my photo blog. I don't know what might be causing it to fail.

      Delete
  4. this was very useful and certainly easier than the one I that I researched, so, thank you.

    ReplyDelete
  5. Thank you so much for for sharing. Is there any way we can have the script Label specific?

    ReplyDelete
    Replies
    1. I think it can but I don't know how to do it yet.

      Delete
  6. it isn't working, just adding "#random" in address bar

    ReplyDelete
  7. I put this in the blog and works great - except in "mobile" mode. If you could make it work in a mobile page it would be really great!

    Thanks!

    ReplyDelete

Post a Comment

Popular posts from this blog

City Code Table for Casio SGW300HD-1AV Watch

How to force only 4G or LTE network connection for Samsung Galaxy Note 4

Mounting NFS share directory from D-link DNS 320L on Ubuntu 12.04 LTS