25-05-2009

jQuery Social media icon script

This simple javascript is used to add a few social media links to your webpage. You can add any social media you like by extending the socialMedia object that contains the data.

This uses jQuery for selecting the element and appending content to it, but you could easily use document.getElementById('elementId').innerHTML = 'bla' instead if you want to include the media links without the jQuery.

drawSocialLinks = function(oContainers){
    	var l,i,socialList = [], socialHtm='';
    	var t = $('h1').eq(0).text();   /*  adjust this to select the title of your article */
    	var u = window.location.href;  /*  this selects the link to your article */
		var iconDirectory = 'images/icons/'; /* this is the director containing your icons */
 
		var socialMedia = [
			{linkText: 'Del-icio-us', icon:'delicious.png',href:'http://del.icio.us/post?url='+u+'&title='+t},
			{linkText: 'Stumbleupon', icon:'stumbleupon.png',href:'http://www.stumbleupon.com/submit?url='+u},
			{linkText: 'Facebook', icon:'facebook.png',href:'http://www.facebook.com/share.php?u='+u},
			{linkText: 'Digg', icon:'digg.png',href:'http://digg.com/submit?phase=2&url='+u+'&title='+t},
			{linkText: 'Twitter', icon:'twitter.png',href:'http://twitter.com/home?status='+u},
			{linkText: 'Google', icon:'google.png',href:'http://www.google.com/bookmarks/mark?op=edit&bkmk='+u}
		];
 
		l = socialMedia.length;
		for (i=0; i<l;i++){
			socialList.push('<li><a href="'+socialMedia[i].href+'" title="'+socialMedia[i].linkText+'"><img src ="'+this.webRoot+iconDirectory+socialMedia[i].icon+'" alt="'+socialMedia[i].linkText+'" /></a></li>');
		}
		socialHtm = '<ul>'+socialList.join("\n")+'</ul>';
		oContainers.append('<div class="socialMediaContainer">'+socialHtm+'</div>'); 
    }
drawSocialLinks($('#whatEverElementId'));
 

Example css -

div.socialMediaContainer{ padding-top:1em; margin-top:2em;  }
#contentHolder div.socialMediaContainer ul li,
#contentHolder div.socialMediaContainer ul{list-style:none;}
#contentHolder div.socialMediaContainer ul li{ display:inline; padding-right:1em}
 

There's a lot of free icon sets you can use for the images:

i used these:

http://www.iconspedia.com/pack/aquaticus-social-1367/

Comments:

8 comments.
Your comment:

»
a 28/07/2009, 9:18 pm
a
thai 02/09/2009, 4:54 pm
socialList :)
LuK 02/09/2009, 9:46 pm
Hey,

thanks for that script, very useful! I wanted to ask if it's possible to "format" the output code a bit...I want the lines to break after each li-tag and if possible to indent the code...in php this can be done via "n" and I also saw this in your code but I don't get it (and maybe the meaning is completly different??)...also found nothing googling =/...I know it's a "useless" detail but I would just like to know how this could be done =)...

thx in advance and keep up the good work!
pingpongsong 02/09/2009, 10:12 pm
with regards to line breaks in code / string concatenation - you can use the same methods as in php in javascript - so adding "\n" would result in a line break in the code
LuK 02/09/2009, 11:54 pm
thanks for the fast answer!

Tried this already but I don't get it^^ =)...where do I have to put the "n" in? For example your code here:
<pre>
socialList.push('<li><a href="'+socialMedia[i].href+'" title="'+socialMedia[i].linkText+'"><img src ="'+iconDirectory+socialMedia[i].icon+'" alt="'+socialMedia[i].linkText+'" /></a></li>');
</pre>

I would like to break it after the /li so I tried to add the linebreaker before the ' and also after the ' but it didn't work in both cases...just too stupid I think =P
pingpongsong 03/09/2009, 8:43 pm
socialList.push('<li><a href="'+socialMedia[i].href+'" title="'+socialMedia[i].linkText+'"><img src ="'+iconDirectory+socialMedia[i].icon+'" alt="'+socialMedia[i].linkText+'" /></a></li>'."\n");

should do it
Eric 09/07/2010, 5:53 pm
How do I call the icons in the HTML?
Juan 14/11/2011, 11:09 am
That is a great looking script, how would I call the buttons up in my html file if I were to add the function to a remote file called something like sociallinks.js

Can you help?

 

[x]