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
    Interview with a Pornhub Web Developer

    Regardless of your stance on pornography, it would be impossible to deny the massive impact the adult website industry has had on pushing the web forward. From pushing the browser's video limits to pushing ads through WebSocket so ad blockers don't detect them, you have...

  • By
    fetch API

    One of the worst kept secrets about AJAX on the web is that the underlying API for it, XMLHttpRequest, wasn't really made for what we've been using it for.  We've done well to create elegant APIs around XHR but we know we can do better.  Our effort to...

Incredible Demos

  • By
    Fullscreen API

    As we move toward more true web applications, our JavaScript APIs are doing their best to keep up.  One very simple but useful new JavaScript API is the Fullscreen API.  The Fullscreen API provides a programmatic way to request fullscreen display from the user, and exit...

  • By
    Fade Images with MooTools LazyLoad

    I recently received an email from a MooTools developer asking a great question about my LazyLoad class: "I'm using your LazyLoad MooTools plugin (which is great, by the way). I have been trying to figure out how to modify it so that once an image scrolls into...

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!