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
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

  • By
    6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

Incredible Demos

  • By
    Create a Dojo Lightbox with dojox.image.Lightbox

    One of the reasons I love the Dojo Toolkit is that it seems to have everything.  No scouring for a plugin from this site and then another plugin from that site to build my application.  Buried within the expansive dojox namespace of Dojo is

  • By
    Animated AJAX Record Deletion Using MooTools

    I'm a huge fan of WordPress' method of individual article deletion. You click the delete link, the menu item animates red, and the item disappears. Here's how to achieve that functionality with MooTools JavaScript. The PHP - Content & Header The following snippet goes at the...

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!