Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook share via FB.ui

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.')
}
like image 383
Andreas Traganidas Avatar asked Mar 13 '14 11:03

Andreas Traganidas


People also ask

What is FB UI?

The method FB. ui() is used to trigger different forms of Facebook created UI dialogs. These dialogs include: The Share Dialog allows someone to post a link or Open Graph story to their profile. The Login Dialog allows someone to use Facebook Login to grant permissions to an app.


1 Answers

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.

like image 158
João Bruno Abou Hatem de Liz Avatar answered Sep 30 '22 14:09

João Bruno Abou Hatem de Liz