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:
Love You
ReplyDeleteLove you too :)
DeleteThnx!
ReplyDeleteYou are welcome.
DeleteThnx
ReplyDeleteThanks bro! *---*
ReplyDeleteThank you so much! I've been looking for this for ages!
ReplyDeletecan i maybe put a picture under the text? y'know, just for decorations.
ReplyDeleteYes.
DeleteHi there,
ReplyDeleteI'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?
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.
Deletethis was very useful and certainly easier than the one I that I researched, so, thank you.
ReplyDeleteThank you so much for for sharing. Is there any way we can have the script Label specific?
ReplyDeleteI think it can but I don't know how to do it yet.
DeleteThnx!
ReplyDeleteit isn't working, just adding "#random" in address bar
ReplyDeleteI love it
ReplyDeleteI 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!
ReplyDeleteThanks!