Random Posts Widget for Blogger

, Posted by ngsk at 4:38 AM

This widget will provide a link for ur users, by each click on this link provides ur users with a different post. It will give ur users a different experience, to eperience this click on "Are ya feeling lucky?" on right side bar of this blog.

How to add this widget to ur blog:

1) Login to ur blog and click on Add Gadget button.

2) Select HTML / Javascript



3) Now copy and paste below code and click on save button


<div id="myLuckyPost">
</div>
<script type="text/javascript">
 
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 feelingLucky(root)
{ var feed = root.feed; var total = parseInt(feed.openSearch$totalResults.$t,10); var luckyNumber = Math.floor(Math.random()*total);luckyNumber++; a = document.createElement('a'); a.href = '#random'; a.rel = luckyNumber; a.onclick = function(){fetchLuck(this.rel);}; a.innerHTML = 'Are ya feeling lucky?'; document.getElementById('myLuckyPost').appendChild(a); } 
</script> <script src="/feeds/posts/summary?max-results=0&alt=json-in-script&callback=feelingLucky">
</script>


Its done.