Facebook share via FB.ui

Andreas Traganidas picture Andreas Traganidas · Mar 13, 2014 · Viewed 34.6k times · Source

I am trying to use the following code that I wrote based on the new documentation for showing a "post to feed" dialog (https://developers.facebook.com/docs/javascript/reference/FB.ui) but i get the following error "ReferenceError: FB is not defined"

the code that i use is the simplest i can come up with:

window.fbAsyncInit = function() {     
    FB.init({
      appId      : 'xxxxxxxx',
      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 = "http://connect.facebook.net/en_US/all.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));


    FB.ui({
        method: 'feed',
        name: 'Facebook Dialogs',
        link: 'https://developers.facebook.com/docs/dialogs/',
        picture: 'http://fbrell.com/f8.jpg',
        caption: 'Reference Documentation',
        description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
    });

Any ideas?

EDIT 1

and if i want to open the dialog when a user clicks on a link i would use jquery click event

$(".userActions a.facebook").click(function() {
   FB.ui({
        method: 'feed',
        name: 'Facebook Dialogs',
        link: 'https://developers.facebook.com/docs/dialogs/',
        picture: 'http://fbrell.com/f8.jpg',
        caption: 'Reference Documentation',
        description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
    });
});

or have the FB.ui inside a function that accepts parameters and call this function e.g.

window.fbAsyncInit = function() {     
    FB.init({
      appId      : 'xxxxxxxx',
      status     : true,
      xfbml      : true
    });

    // Code in here will run once FB has been initialised

    function FB_post_feed(method,name,link,picture,caption,description){
       FB.ui({
            method: method,
            name: name,
            link: link,
            picture: picture,
            caption: caption,
            description: description
        });
    }

};

(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 = "http://connect.facebook.net/en_US/all.js";
   fjs.parentNode.insertBefore(js, fjs);
 }(document, 'script', 'facebook-jssdk'));

and somewhere in the HTML

$(".userActions a.facebook").click(function() {
    FB_post_feed('feed','Facebook Dialogs','https://developers.facebook.com/docs/dialogs/','http://fbrell.com/f8.jpg','Reference Documentation','Dialogs provide a simple, consistent interface for applications to interface with users.')
}

Answer

Ok, there are a few conceptual mistakes you've done there.

First:

window.fbAsyncInit = function() {     
    FB.init({
        appId      : 'xxxxxxxx',
        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 = "http://connect.facebook.net/en_US/all.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

FB.ui({
    method: 'feed',
    name: 'Facebook Dialogs',
    link: 'https://developers.facebook.com/docs/dialogs/',
    picture: 'http://fbrell.com/f8.jpg',
    caption: 'Reference Documentation',
    description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
});

What is this supposed to do? Open a share dialog without user interaction? FB.ui must be called when Share Dialog is supposed to appear, not right after Facebook AsyncInit function.

And, also, facebook sdk will be started Asynchronously, as the name of the function suggests. It means FB will not be defined in the place you put the function.

Second:

window.fbAsyncInit = function() {     
    FB.init({
        appId      : 'xxxxxxxx',
        status     : true,
        xfbml      : true
    });

    // Code in here will run once FB has been initialised

    function FB_post_feed(method,name,link,picture,caption,description){
        FB.ui({
            method: method,
            name: name,
            link: link,
            picture: picture,
            caption: caption,
            description: description
        });
    }
};

FB_post_feed function, in this case, is a local function inside fbAsyncInit function. So, you don't have access to FB_post_feed function outside fbAsyncInit.

Besides, FB.init is asynchronous, which means FB will be undefined by the time FB_post_feed is being created.

Depending on how your HTML code is defined, and if this identifier is correct, the code

$(".userActions a.facebook").click(function() {
    FB.ui({
        method: 'feed',
        name: 'Facebook Dialogs',
        link: 'https://developers.facebook.com/docs/dialogs/',
        picture: 'http://fbrell.com/f8.jpg',
        caption: 'Reference Documentation',
        description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
    });
});

should work fine. However, just an advice: Classes are usually used to style something. It would be best-practice if you use the button(or "a" element) id instead.