Use FB.ui() instead of old Facebook sharer

April 22nd, 2014 by Alex Leave a reply »

So the old lovely facebook.com/sharer.php no longer treats all your titles and summaries, and you don’t have a possibility to use og: meta tags. Same problem? Well, truth is that platform developers are also developers and it’s hard to make something perfect initially. That’s why things constantly change, and this time we at least have a substitute: Facebook JavaScript SDK

There will be 3 steps we need to do in order to make it work.

Step 1: Register your application
We will need the APP ID when we are done with registering. Make sure you use the same domain name as you are testing on, otherwise you get an error looking like this: API Error Description: The specified URL is not owned by the application

Step 2: Init the SDK:
Put the code right after your <body>, and make sure to change {your-app-id}

<div id="fb-root"></div>
<script>
      window.fbAsyncInit = function() {
        FB.init({
          appId      : '{your-app-id}',
          status     : true,
          xfbml      : true
        });
      };
 
      (function(d, s, id){
         var js, fjs = d.getElementsByTagName(s)[0];
         if (d.getElementById(id)) {return;}
         js = d.createElement(s); js.id = id;
         js.src = "//connect.facebook.net/en_US/all.js";
         fjs.parentNode.insertBefore(js, fjs);
       }(document, 'script', 'facebook-jssdk'));
</script>

Step 3: Call the FB.ui() when you need to share something
Example with jQuery:

$(function() {
  $('a').click(function() {
    FB.ui({
      method: 'feed',
      name: 'Facebook Dialogs',
      link: document.location.href,
      caption: document.title,
      description: 'Some custom text here'
    });
    return false;
  });
});

2 comments

  1. Toni says:

    method: ‘feed’ is deprecated,
    now use method: ‘share’ or method: ‘share_open_graph’

  2. Alex says:

    nice… a couple months passed and now something changed again