Blogger Feeling Lucky Widget (Random Post)

UPDATE Sep 23, 2008. If you have previously installed this script recent changes at Blogger have caused it to break. I have updated the installation page to fix things, and I've outlined what you need to do at the post Random Post Widget Fix. If you have never installed the script before, then you can follow the instructions as normal as they have been updated.

I wouldn't have thought many people would think something like this would be useful, but a recent post on Techcrunch goes ape over wordpress having a 'random post' widget now. I figured if wordpress has one, New Blogger should as well.

With that in mind, here is the Blogger Feeling Lucky widget. Since we are in the Google family, I thought using the 'feeling lucky' nomenclature was appropriate. It's a silly little widget that adds a link to a random post on your blog. It is available in one-click add a widget to your Layouts template, or for classic templates the raw code is available to add that works as well. Also, a slightly tweaked version of the code also allows FTP Blogger blogs to join in the fun. All the things you need to install are explained on the setup page, although it's pretty simple. Currently I have it installed under the sidebar search box, via the manual code provided for exact placement.

Blogger Feeling Lucky Widget Install Page



Ricardo Santos has made a Portuguese translation available at Widgets para o blogger - Phydeaux3

Notes

For those that may be interested, here's how it works. Nothing really earth shattering going on. In the initial page load, the scripts make a json call to the feed url. Here I'm only interested in getting the total number of posts in the blog, so the call is made to the summary feed (to keep load time minimal) and with only one entry returned. The total number of posts is returned in New Blogger in the openSearch:totalResults field, so take the total number of posts and then use standard means to generate a random number within that range. Then the link is created using that number as it's variable.

Now at this point we don't have a valid url yet, just a number. So when the link is clicked, another quick json call is made, using the random number generated as the start-index parameter to get that entry in the feed only, a quick parse of the results to grab that entries url, then good old document.location (Thanks to Pete changed to window.location and a few other small tweaks to keep the back button working) to switch to it.

Old Blogger did have a similar tweak by Mario Brhemenz, but like a lot of our Old Blogger stuff relied on an outside service (del.icio.us)to work. I don't know that I've seen one for New Blogger before.

I think everything works as it should, but if you find any issues lemme know.

81 comments:

Pete said...

Trés slick.

But unfortunately it breaks the "Back" button. Perhaps window.location rather than document.location will do the trick?

phydeaux3 said...

D'oh! See what happens when amateurs start messing with things. :-)

Thanks for the heads up. It seems that IE7 and Opera9 had no problem with it, but Firefox definitely didn't like it (I don't even try to guess about what Safari thinks)

Even just a straight swap of document.location to window.location didn't actually do it as one might think, but that's probably a result of something else I was doing wrong. Finally hit on the right combination with window that seems to work. Of course, I won't be too surprised if I broke something else in the process.

So (hopefully) it's fixed now.

Hellowin said...

:) :) :)
nice hack
but can you tell me how to create a link so that i can put it other places also
i mean i have installed code and working fine. but i want to create href link so that i can place it at navigation bar
i tried to use it at my header but not working you can just take a look at it
http://techmake.blogspot.com/#random

phydeaux3 said...

@hellowin

Hmm. Well it wouldn't be that difficult to modify the script, but unfortunately I just don't have any time to look the next few days. As soon as I can I'll take a look at providing a way to integrate that into an existing link set that others may also find useful...so check back later.

phydeaux3 said...

I've now changed around the coding just a bit, so it's easier to put the link wherever you want (as per hellowins needs). New stuff is updated on the installation page.

the Finnie's said...

Great hack, thanks!
One question; I want to be able to use target="_blank" to make it open in a new window/tab, where do I put the code?

Ta

phydeaux3 said...

where do I put the code?

You don't. :-)

Since the page is called via javascript, just adding a target="_blank" to the link wouldn't work. You'd have to modify the script to open in a new window, which I'm not really interested in. Why open a link to your blog in another window?

Plus I spent enough time making sure the back button didn't break. If you want it to open in a new window, that's left as an exercise to the reader. :-D

நாகு (Nagu) said...

Very nice. It's a good feature.

Sam Chan said...

Love this widget. It is very useful for my blog. Thank you very much.

Alex (intellimind) said...

i have been looking for this type of widget forever. thank you plus your whole site is amazing.

Do you mind if i mention you on my blog?


http://intothemindd.blogspot.com

Dave said...

Nice and clean both in code and when displayed.

Dave

poprave said...

hi, do you know if it does exist a similar widget who shows random images from the archive instead of links?
i've been looking for it everywhere but i found nothing and i think it would be a great tool, because the reader sees a picture and by clicking it he goes to the related post.

I found an example of this on thesuperficial.com, on the sidebar "so freaking hot".

thanks,

poprave

ilker said...

Hello there.. I tried implementing your hack and placed the random link in the sidebar but it doesn't work. I tried placing both codes (classic template and FTP) but still doesn't work. What did I do wrong? Can you please help!!

Jonathan Fung said...

Wow. That was very simple to install. And it seems to work quite well. My only request would be for a widget that lists a certain number of random posts, rather than just directing the reader to one. But great work!

Dave Dragon said...

Yet another fine hack!

Installed cleanly and works perfectly.

Thank You!

-Dave Dragon
Ride it like you stole it

Leo Matheus said...

Nice widget!

Is there a way to use it to just show pages with a given label?
For example, in my blog I have labels "poetry" and "other stuff", and want the "Feeling luky" to display just the "poetry" ones... Is it an easy adaptation?

Thanks,

Leo Matheus said...

Yuhu! It's me again.

I just made the "Feeling luky" show posts from just one Label. Just had do modify the "/feeds/posts/summary" to "/feeds/posts/summary/-/Label" and it worked without problems.

Dave Dragon said...

Is there a way to call the random post link directly from the URL instead of having to click the link?

as an example

http://davedragon.rilysi.com/?Do_Random


Thanks

phydeaux3 said...

No, not the way things are currently setup.

Anonymous said...

Works just fine! Thanks for this! Because of this post I added you to my Useful Link list ;)

Cheers!

Flickzzz.com

LBC said...

I think this is an awesome widget! I implemented it on a beta re-design of my blog and it worked perfectly. Then when I was ready to use it on my "real" blog, the headline (I chose "Feeling Lucky") shows up, but not the actual widget. Would the number of posts on the blog have anything to do with it? My beta blog has less than 100; my real blog has over 800 posts.

LBC said...

Ignore my earlier post! I had turned off the site feed. D'oh.

Anando said...

nice hack. Thanks. But there should appear the title text of the random text as the link text, not the same fixed words !!!

Anyways, always and in always thanking you. TAke care.

Mythreyee said...

Such a wonderful widget. I added it to my site. thanks.

jasszzzz said...

Thanks for the widget, just one thing can you tell me how to make it appear a button and not just a link?
Thanks right away..

Dana said...

The -add to blogger- thing didn't worked for me, but I tried the classic mode (adding the code in the {head} of the template) and worked very weel!

Thanks a lot for this widget! :-)

Cheffo said...

Hi, I introduced this widget in Bulgarian. I also changed it to show the title of the selected post. Code is available on http://nashcheffo.blogspot.com/2008/01/blog-post.html

Comments are welcome.

Rubén said...

Please check out my problem with this widget. I put all the code exactly as you said (also I tested the auto-installer), but anything works, and when I click on the "random post" button, only changes the address of my blog and it become the same ended with /#random
I´m doing anything wrong???
Please let me know
you can check it on www.barriodebenalua.blogspot.com on the right, under the cloudtag

phydeaux3 said...

@ruben

I'm not sure how it happened, but the script portion has an error in it. It's not exactly like the one on the instructions (missing a few characters in at least one line). Not sure if it happened in the copy/paste or what. I would suggest re-copying/pasting just the script portion from the instruction page again to make sure all errors are corrected.

Rubén said...

Thank you very much for your answer. I´ve copied now exactly as the instalation page showed me. And everything works as the same way: when I click on it, the home page of the blog appears.
I´ve tried on other blogs this button, and anyone works. Did you know that? or it´s my web browser?? -firefox2-
On your web, when I click on the upper link called "feeling lucky?" under the search box, it doesn´t takes me to a random post, it sends me to the same page.

phydeaux3 said...

@ruben

It's working for me just now on your blog, the script has been corrected. You MAY need to clear your browsers cache so it's not showing the old/bad version.

As far as it working, it works for me here as well, and always has. And I use Firefox also.

Rubén said...

That was wonderful!! now it works!! I cleared my firefox cache and now it gives me the random post!! thank you very much!!
now, should I suggest clear the cache to all my visitors??

Sagar said...

There is an error saying "The element type b:skin must be terminated by the matching end tag". Please solve my problem

BlogHer said...

Hello,

I'm using Minima Stretch (in case that matters) and trying to place the widget in Layout under Javascript but not having any luck with getting it to work. I am afraid to fiddle with the main template. I've cleared my cache in Firefox. I tried the first way and the second on your installation page. I've read your replies to other users. Still not lucky.

From this, can you spot what I've done wrong? Thank you.

phydeaux3 said...

@blogher

Blogger is giving me an error when trying to view your profile, if you are still having the issue leave me the direct link to your blog and I'll try to take a look.

BlogHer said...

http://agnosticseeker.blogspot.com/

Thank you!

Droplet said...

Thanks! I've been looking everywhere.

Jane Air said...

I don't know why it doesn't work.
I tried the 2 methods.

If you have some time, could you have a look ?
http://aufilduweb.blogspot.com

Thank you.

BlogHer said...

Maybe what you're supposed to do is add your blog's ID somewhere in the widget's HTML?

When I click the icon "Add to Blogger" there are the choices to Edit Content and Edit Template. Looks like we're supposed to change something in those?

In the meantime I'm just picking out some posts that I like and numbering them.

http://agnosticseeker.blogspot.com/

phydeaux3 said...

@jane air

I've looked twice at your blog and have never seen anything installed, so I can't comment on why you were having trouble

@blogher

Same for you. I've looked a couple of times since your first comments and I've never seen anything installed, so can't begin to guess.


When I click the icon "Add to Blogger" there are the choices to Edit Content and Edit Template. Looks like we're supposed to change something in those?


No, you are not supposed to change anything in those.

BlogHer said...

I've placed it there again, top right sidebar.

http://agnosticseeker.blogspot.com/

Thanks!

phydeaux3 said...

@blogher

Ok, well this would be the problem. You don't have feeds enabled.
The feed specified by http://www.blogger.com/feeds/29249051/posts/default is not enabled

The second line on the instructions page says that feeds have to be enabled on the blog. You can choose either short or full feeds, it works off your public feeds.

Blogger help on enabling feeds

Try that and see if'n it works.

BlogHer said...

Oh wow! That was so easy. LOL It works just fine!!

Thank you,
Clarice

Ankit Desai said...

thanks so much for this...

nerodha said...

this is excellent. works perfectly placing an image instead of just a link too. thanks again for your stupendous work ;)

Anonymous said...

Is there a way to add a button to the script source widget, rather than link text?

Where would I put it?

JD at I Do Things said...

Would this work on a non-blogger blog? I love it on the blogs I've seen and would like to add it to mine.

WebbieStuffs said...

Poor me. it has been more than a year and i still dont have a clue what is this about. am gonna try now this 'feeling lucky' stuff and will be returning here for feedback.

Wanderer... said...

Just installed it and it looks great in my sidebar. Great idea and thanks for sharing them so smoothly to all of us bloggers.

WebbieStuffs@http://www.webbiestuffs.com said...

Whatta great innovation by blogger this feeling lucky thing. I have installed mine already. Thank you for the help of this post.

Budgets are Sexy. said...

IT WORKED PERFECTLY!!! Thanks so much, been thinking of doing this for a while, just never got off my a$$ to try it ;)

Glad i came across your site first!

:: Suzanne :: said...

love it! Thanks so much.

:: Suzanne :: said...

love it! Thank you so much.

:: Suzanne :: said...

THank you - I installed it and credited you in my "credits" section.

(fido3?)

Pedro Moreno said...

Thank you, thank you, thank you!

Crabby McSlacker said...

I installed this a couple of days ago; it worked great, then suddenly stopped functioning normally. It took me to some weird page that seems to be a feed subscription link for comments.

I'm afraid I had to take it off so linking to my blog won't help diagnose the problem. I just wanted to find out if this was a particular issue with my blog or if others are experiencing problems too.

Thanks, it was an awesome widget!

Stud kickass said...

For some reason, mine just won't call up any random page. It literally was fine yesterday. It brings up a, "open this program with which application window" instead. This is in firefox.

Kct said...

Why the "Feeling Lucky?" stop work suddenly? Now is showing the comments feed, but not the post feed. What happened?

phydeaux3 said...

For those having issues with this, I gave a brief explanation why at this post.

Calendar Random Post Busted

I'm going to try to have a fix for the random post widget up shortly.

Kct said...

Hi phydeaux3!
I notice that your "Feeling lucky" is work again. How did you fix the problem?

phydeaux3 said...

Ok guys and gals, if you have been having problems head over to the latest at Random Post Widget Fix where I give a brief explanation of how to fix it.

Hopefully it shouldn't be too hard to redo for you and this should fix it. Sorry for the troubles.

Alexis said...

Hi!

I just fixed my RandomPost widget and I am so grateful! Thanx man!

However, I got another problem as well.. Around 2 weeks ago, I got TWO problems with my blog:

1) the first problem was the RandomPost, and (as I said) with your recent post, I fixed it...

2) The second problem is this: when I created my blog, I played a bit with the HTML (minima lefty stretch) and added a third column on the right... Everything was working smoothly for about 7 months, when two weeks ago (thats when the RandomPost problem appeared too) the Middle column (the one containing the actual post) started to appear alligned exactly BELOW the Left column... and the Right column followed as well, appearing next to it (way down the page, but at least aligned correctly, to the right of the page)

Its as if something is pushing everything except the Left column down... I have no idea what is causing this, and I would very much appreciate if you (or anyone else) could help...

the address is:
http://my-daily-laughing-dose.blogspot.com/

thanx so much

alex

Simos Segiopoulos said...

Thank so much for the update.

This widget is really useful and my readers love it! So do i!

Wishing you the best!

Simos

HansThane said...

thanks for this! just what i needed! :))

Anonymous said...

How can you get it to display the title of the random post instead of 'do you feel lucky punk?'?

- Alvin

povilli said...

Thanx very good added my blogger

Rajesh N Butpur said...

nice hack thanks



blogger-godown.blogspot.com

nerodha said...

fantastic my friend. the fix did it. thanks so much for your work!!

durandal said...

Thank you for this script. I had this idea for a loooong time, but I assumed it would require manual editing every time I published a new article. Not so.

Here it is in action (right column, 1 screen down).

http://elcantardelalluvia.cl

d.

Altuğ KOÇ said...

Greetings,

Thanks for the great widget which u shared with us Pyhdeaux3!

I'm now wondering about, can't we make a "random posts' links list" code for the sidebars to list few posts' links randomly from our archive which basis on your "feeling lucky" code...?

Softwaremu said...

Nice Widget..
Thanks n Success

Schmitzl said...

This a great widget. Installed it and it works. Been trying to offer the link as an image. Changed the inner html and this works great in Firefox. IE however, doesn't show any images when I do this. Any ideas? Does the inner html need specific coding to show images in Internet Explorer? Thanks!

phydeaux3 said...

Schmitzl-

Hmmm. It should work with an image. I looked at what you have, and you did seem to add more than just an image tag in there. Still not sure why it works in Firefox and not IE, but can you try removing it, then reinstalling with JUST the image tag (no extra a and div tags) to see if that works.

I tested it out with just your image and it works in my tests in both browsers.

khengsiong said...

Is there a one-click add to blogger layout? I click 'Add a Gadget' search for 'Feeling Lucky' and 'random post', but haven't found it.

phydeaux3 said...

@khengsiong

There is nothing to search for. On the installation page I made there is an add to blogger link.

http://phydeauxredux.googlepages.com/Blogger-Feeling-Lucky.html

natalika said...

Hi!

I've been looking for something like this for a while. Have put it now on one of my blogs, just for testing and I'am afraid I'm experiencing some trouble, can you help?

URL: http://halbeflamme.blogspot.com

With Firefox 3.0.5 it seems to run properly, a great thing!

But then with IE 7 it works a couple of times and then the site hangs and the random link doesn´t work anymore and, which disturbs me the most, I cannot even leave the site (either by typing another URL nor when I try to reach another site through my favorites) and when I try to click another link within my blog it just takes ages to load (acutally it doesn't load at all but hangs).

Finally when I give up and try to close the tab I get a pop up window telling me it could be a problem to do so since the site is still working on something. Sometimes I just get a pop-up window saying the site isn't available at the moment...

Any hint what the problem is??

natalika said...

I found a solution for my problem with your widget and the IE:

I use a Site Meter counter which started making trouble today after I copied your widget to my layout (this annoying specificclick.net cookie from them was making my site really slow and somehow it also kinda broke your widget). I have changed my counter from javascript to simple html and now the widget works superbly (and I avoid this cookie shit from tracking everything about my site).

Just wnated to let you know.

Thanks for sharing the widget, I love it.

Indonesia Sexiest Women said...

Cool, nice widget dude.this widget can help me to get more reader to my blog.so they not boring anymore.Thanks

T.Vaughan said...

Thanks! New to blogging, but I NEEDED this thing. If you saw my amateur blog, you'd see why. (vismsiv.blogspot) Saw others, but like this best.
By the way, good eye.

sas said...

i have tried to add an image rather than the 'are you feeling lucky punk' text.

i am using photobucket for the html code.

i either get no image (if I use the IMG code) or the full html link but no image if I use the IMG Thumb code.

any help would be greatly appreciated!

Anonymous said...

Thank you !!!

You are my personal hero !