Making Flickr Badges Open New Window

Posted Monday, August 31, 2009 11:24:05 AM
I recently added a Flickr Badge to the header of my blog and really loved everything about it except that... there was no way to have it open the photos in a new window when they were clicked. Because the code was written by Flickr I had no way to modify it before it was created, but I did afterwards. Here is the code Flickr gave me to add the badge to my site minus some extra "fluff" code that I've removed for the sake of shortening this post:
<table cellpadding="0" cellspacing="0" border="0" id="flickr_badge_wrapper">
	<script type="text/javascript" src="

So, I simply wrote a script that would loop back through all of the links within the table "flickr_badge_wrapper" and change their "target" property to "_blank". Here is the code.
	var oFlickrTable = document.getElementById("flickr_badge_wrapper");

	oFlickrBadgePhotos = oFlickrTable.getElementsByTagName("a");

	for (nBadgePhoto = 0; nBadgePhoto < oFlickrBadgePhotos.length; nBadgePhoto++) {

		oFlickrBadgePhotos[nBadgePhoto].target = "_blank";

Feel free to use this code yourself, just be sure that if you've renamed "flickr_badge_wrapper" (or Flickr changed the name by the time you read this article) that you update my code. Enjoy!
Comments: 34 (View | Post)
Categories: Most Popular, Technical

Post A Comment

View Comments

There are 34 comments on this article
Vasilis (7 years ago)
You RULE !!!
Artur Tavares (7 years ago)
Sir, you're a genius!!! Thank you so much, that's exactly what I was looking for! Have a great day!
Mich (10 years ago)
hi !
Thank you, i adapted your code for my WP site :

<script type="text/javascript" >

var oFlickrTable = document.getElementsByClassName("flickwrap");//now there is only class parameter
oFlickrBadgePhotos = oFlickrTable[0].getElementsByTagName("a");
for (nBadgePhoto = 0; nBadgePhoto < oFlickrBadgePhotos.length; nBadgePhoto++) {

oFlickrBadgePhotos[nBadgePhoto].target = "_blank";

popy5 (11 years ago)
BWonderful! Thank you so much!
I used this code on my site and it worked like a charm.
Thanks for sharing this code!
David (11 years ago)
Awesome! Thanks for posting this!
Jacquii Cooke (11 years ago)
Lovely bit of code. I'm using it at = Thanks so much!
Felicia (11 years ago)
Thanks so much!!! Worked like a charm.
JL Johnson (11 years ago)
You sir, are genius! Used this over at
Chris (11 years ago)
Worked perfectly.
Heather (12 years ago)
It worked! Thank you so much:)
Jared (12 years ago)
Super helpful - Cheers
Adam (12 years ago)
Your a star!
Thank you
Alan North (13 years ago)
Adam, Very many thanks. Works a treat...I will use it a lot.
Thanks also for the family photos. As a doting grandfather I have enjoyed them.
noe4laj (13 years ago)
Thanks for posting far the easiest explanation of how to resolve this issue that I found. Worked like a charm!
Studio Hyperset, Inc. (13 years ago)
Super helpful. Thanks!
Ron (13 years ago)
Adam, thanks for helping me out with not only this code but the additional code!!

Adding to the last line...

oFlickrBadgePhotos[nBadgePhoto].href = "";

..still allows for random image display yet also brings the user to the main collections page no matter which image they click. Thanks again for giving me this additional piece of code.

anthony (13 years ago)
now that was easy
Alex (13 years ago)
Shivangi (13 years ago)
Thanks a lot
This Helped me
g.s. (13 years ago)
Tavis Steenbeke (13 years ago)
Do you know how to do this for LinkedIn badge on wordpress?
Christa (13 years ago)
Seriously Thanks. I looked for the answer to this for several hours!
Miguel Neto, Rio de Janeiro Br (13 years ago)
Muito bom! (Very Good!)
It works! But only if put it after flickr's script...
(Funciona! Mas somente se for colocado após o script do Flickr).
Muito obrigado (Thanks!)
prophet (14 years ago)
Thank you man, exactyle what i was looking for.
used it on my new blog

Sandy (14 years ago)
Wonderful! Thank you so much!
Janderson (14 years ago)
Thank you very much!
Eugenio (14 years ago)
Great script! It works [but only if you put it after flickr's script] and it is fantastic.

Thank you,
Paul Altobelli (14 years ago)
I used this code on my site and it worked like a charm. Thank you!
Victor (14 years ago)
This patch works great.
Kate (14 years ago)
I'd been annoyed for ages by the flickr badge making people leave my site and just looked up your solution now. Works great, thanks!
Adam Lewis (14 years ago)
Sounds like there may be an issue with an unclosed tag somewhere in your HTML. If you'd like, drop me and email with your code and/or the URL of the page it's on and I'll take a peek.
justin bieber (14 years ago)
It only works for the first picture. The rests open in the same window.
Mirta (14 years ago)
Well done! ;)
Elaine (14 years ago)
awesome. thanks for this.