Social sharing sites like Facebook, Twitter, and Google Plus are essential for people who blog like myself.  Why write about something you’re passionate about if no one can find the article (purposely or by chance?)  People share enough of your posts and you get noticed and get a sweet job at Mozilla…in my case.

What I don’t like about the social sharing sites are their widgets:  they load very slowly, you can’t customize how they look, and they allow social sites to track which sites you’re viewing.  Slow, ugly, and invasive — yuck.  You can, however, create social sharing links of your own, making sure to open the sharing window using some custom JavaScript.  Here’s how!

The JavaScript

I’ve separated each major share site into it’s own function:

function shareTwitter(url, text) {
  open('http://twitter.com/share?url=' + url + '&text=' + text, 'tshare', 'height=400,width=550,resizable=1,toolbar=0,menubar=0,status=0,location=0');  
}

function shareFacebook(url, text, image) {
  open('http://facebook.com/sharer.php?s=100&p[url]=' + url + '&p[images][0]=' + image + '&p[title]=' + text, 'fbshare', 'height=380,width=660,resizable=0,toolbar=0,menubar=0,status=0,location=0,scrollbars=0');
}

function shareGooglePlus(url) {
  open('https://plus.google.com/share?url=' + url, 'gshare', 'height=270,width=630,resizable=0,toolbar=0,menubar=0,status=0,location=0,scrollbars=0');
}

You can add click listeners or use event delegation to call each function when a share button is clicked.  If you want to display the share count for each service, I’ve got a recipe for getting Twitter and Facebook share counts via JSONP.

Using your own custom styles and the functions above, you can create elegant social sharing buttons without using each service’s widget.  Your site becomes faster, more stylish, and respects your user’s privacy.  Score!

Source: David Walsh

Leave a Reply