Learn to embed social sharing buttons the correct way

May 25, 2015 12:51 GMT  ·  By

Social sharing buttons are everywhere and even if most webmasters look at them with scoff, users have gotten accustomed to using them and will actively look for them on any page if they believe its content is good or interesting enough for being shared.

All social networks provide embed codes for adding their own share button to remote pages, and while highly useful for non-technical users, these buttons are a nightmare for experienced Web developers.

The problem is that these social button embed codes do not only create a fully-functional button, but they also tend to load external resources in the form of JS&CSS code. These are “page blocking” resources that delay the page loading process and tend to make every website behave slower than it really is.

Since every social network that utilizes this kind of social sharing buttons uses a custom-made share screen where the details of each shared link can be tweaked, the solution to this mess is to use custom-made buttons that redirect the user to that screen, without loading all those useless third-party scripts.

There are quite a few good alternatives available

Thanks to the open source community, you don't even have to know how to do this. There are lots of scripts on the market providing some pretty good tools for this problem already.

There is jsSocials, Ridiculously Responsive Social Sharing Buttons, HTML-Only Share Buttons, prettySocial, Flati Social Share, socialShare.js, and Angular Socialshare. All of them are available for free, will work even with JavaScript turned off, and will load social sharing buttons the proper way, without any third-party scripts.

socialShare.js also provides share counters
socialShare.js also provides share counters

Doing it yourself

If you're one of those developers that's always picking on the quality of someone else's code, then don't worry, there's a way of recreating their results in a simple manner.

First off, you need to get your hands on the URL for the intermediary share screen of the social networks you want to build a button for. This is a small list of sharing URLs for all major social networks.

code
http://www.facebook.com/sharer.php?u=YOUR_URL
https://twitter.com/share?url=YOUR_URL
https://plus.google.com/share?url=YOUR_URL
https://pinterest.com/pin/create/bookmarklet/?media=IMAGE_URL&url=YOUR_URL
http://www.linkedin.com/shareArticle?url=YOUR_URL
http://digg.com/submit?url=YOUR_URL
http://reddit.com/submit?url=YOUR_URL
If you want to create buttons for other networks, then you'll have to search for them yourself. Trust us, it's not as difficult as it looks.

The code for your social share links should be something like this:

code
<a href="https://twitter.com/share?url=YOUR_URL">Twitter</a>
Keep in mind that the URL you want to share must be passed in an encoded form. Since every page runs JavaScript, the encodeURI(); function can be used to take the current URL and convert it into its encoded form, and then passed to the button.

This function also has its alternatives in almost all programming languages, so you can create the encoded URL on the server-side and embed it on the page automatically when sending the HTML code to the user. There's also a visual tool from Eric Meyer that allows developers to perform this operation by hand (via copy-paste).

Once you have the final code, it should look like this:

code
<a href="https://twitter.com/share?url=http%3A%2F%2Fsoftpedia.com%2F ">Twitter</a>
This produces a link which, when clicked, shares the user's desired URL on Twitter. Keep in mind that Twitter and other social networks also support additional parameters like thumbnail images, preset titles, preset descriptions, video URLs, and so on.

If you want to produce social sharing links more advanced than this simple form, take a look through each website's developer section and see what extra parameters each network supports.

PS: You'll need some CSS to style your buttons. They normally produce a text-based link. If you want them to look pretty you'll also have to write a beautiful skin for them as well.

Hope this was clear enough and you understood why it's important to use custom-made solutions for social sharing buttons and avoid using the bulky, page-blocking tools provided by default. Happy coding!

Social sharing buttons embedded the right way (6 Images)

jsSocials comes with lots of CSS skins
Ridiculously Responsive Social Sharing Buttons can be used with mobile-friendly websitesFlati Social Share works as a toolbar on the top of your site
+3more