Random Post Button

By KillerElf on 7:20 AM

Filed Under:

To get a Random Post button on your blogger website first login to your blogger dashboard , then navigate to your blog , go to layout then add a gadget.Find HTML/JavaScript under Basic and click the plus sign next to it.Name it Random Post or whatever you want it doesn't matter because the name of the button is in the script itself this is just so you know what gadget it is.


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

Paste this script in where it says content , save it and move it to wherever you want it to be on your blog.To change what the button says just replace View Random Post in the script I've made it bold so it's easier to find.

0 comments for this post

Post a Comment