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/
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!
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
should do it
Can you help?