Content feed Comments Feed

How to add Random Posts Widget for Blog?

Posted by Wonderful-Tricks
ADVERTISEMENTS

How to add Random Posts Widget for Blog?
Seen a lot people finding a Random Post widget for there blog? Then thought should have an tutorial on how to install it too in the blog.Well, here it is. If you want to know what it do is, whenever a reader click on link created by this widget all the times a different post will open for the reader in the blog as by luck.

Now lets see how to install it in your blog as it is very simple.

Go to Page Elements and click on Add a Gadget on the sidebar. Select HTML/Javascript and add the javascript code given below and save the widget/gadget.


<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, punk?'; document.getElementById('myLuckyPost').appendChild(a); } </script> <script src="/feeds/posts/summary?max-results=0&alt=json-in-script&callback=feelingLucky"></script>

I hope you will install it very easily Good Luck!!

Your Ad Here

0 comments

Post a Comment

Blogumulus by Wonderful-Tricks

Followers