Social Sharing Links without Widgets

By  on  

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!

Recent Features

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

  • By
    Animated 3D Flipping Menu with CSS

    CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more.  I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...

Incredible Demos

  • By
    Highlighter: A MooTools Search & Highlight Plugin

    Searching within the page is a major browser functionality, but what if we could code a search box in JavaScript that would do the same thing? I set out to do that using MooTools and ended up with a pretty decent solution. The MooTools JavaScript Class The...

  • By
    Implementing Basic and Fancy Show/Hide in MooTools 1.2

    One of the great parts of MooTools is that the library itself allows for maximum flexibility within its provided classes. You can see evidence of this in the "Class" class' implement method. Using the implement method, you can add your own methods to...

Discussion

  1. I’ve been working on something similar, and I started off looking for this very post… But that was a couple of weeks ago. I guess your blog is just what comes to mind first when I’m trying to think of helpful JavaScript.

    What I came up with was similar, but involved URL & URLSearchParams. Unfortunately, those are not supported so well. Even Chrome doesn’t have URL.SearchParams

  2. It is returning me Twitter API error. Any guess?

    • Works well for me. Which browser/device?

    • Chrome on Windows8

  3. Hey, you should be using https:// links to Twitter and Facebook. They work too, and are better for user privacy.

    I use slightly different links within my own website for Twitter and Facebook as well – the Twitter one uses an intent link, the Facebook one has a different thing too. https://media.info/radio – the ‘share’ links are on the right-hand side.

  4. Great post!

    I tried using these links on my iPhone, but the Facebook one didn’t quite work as expected. It loads in a new page/tab, but none of the information is carried over. Any ideas?

  5. Aicke Schulz

    I’m not sure if I get it right, but custom sharing plugins, which prevent the initial widget loading, are out there a long time, so this tip feels a bit oldschool. Sorry, if I’m wrong.
    One example is “SocialSharePrivacy” initially from a german tech news page, with forks like https://github.com/patrickheck/socialshareprivacy/ or with lot more options https://github.com/panzi/SocialSharePrivacy.

    Second, maybe for your interest, the mentioned (unofficial) sharing count api for twitter, will be removed next month, see https://twittercommunity.com/t/a-new-design-for-tweet-and-follow-buttons/52791.
    “The Tweet button has displayed share count over the last five years by querying a JSON endpoint hosted on various domains. These private JSON endpoints have been used by third-party developers over the years to retrieve a simple share count of any URL. These endpoints will be shut down next month when the Tweet button removes its share count feature.”

  6. It is returning me Twitter API error. Any guess?

  7. Jai

    Hey, the Facebook share in this example and on the site doesn’t work on mobile Safari, iOS

    • Jai

      This is the workaround I found that works for sharing to Facebook on mobile

      open('https://m.facebook.com/sharer/sharer.php?u=' + urlLocation + '&picture=' + image + '&description=' + text, 'facebookshare');
      

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!