Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook JavaScript Send Dialog, callback function works incorrect

I'm trying to execute callback function in FB.ui (send dialog). It called in same moment when loaded FB.ui but I want execute callback function after pressing 'send' or 'cancel' button. Is it realizable?

function callback(response) {
      alert('message was sent');
}

FB.ui({
                method: 'send',
                name: 'Dialog',
                link: 'http://***.com',
                redirect_uri: '****',
                description: '***',
                picture: '***',
                to: userId
            },
                callback(response)
            );
like image 974
Al Puz Avatar asked Feb 07 '12 12:02

Al Puz


2 Answers

The callback requires a function as a parameter. Here, you are actually calling the function.

Facebook will actually call the function that you pass it along with the response.

Rather than passing "callback(response)" as the callback parameter, just pass "callback" like this:

function callback(response) {
      alert('message was sent');
}

FB.ui({
       method: 'send',
       name: 'Dialog',
       link: 'http://***.com',
       redirect_uri: '****',
       description: '***',
       picture: '***',
       to: userId
       },
       callback
);
like image 191
Fisch Avatar answered Sep 19 '22 03:09

Fisch


Issue has probably been solved or is irrelevant to OP, but this might help some others out that stumble upon the post.

Similar to Mike Jerema's comment on Fisch's reply (Unable to comment there due to insufficient rep):

The response callback receive from the user interacting with the send dialog returns one of three things:

  1. If Send is clicked an empty array is passed back (instanceof Object)
  2. If Cancel is clicked null is returned
  3. If the 'X' in the corner of the dialog is clicked, undefined is returned.

Therefore the correct code to handle this callback for all use cases would be:

var callback = function(response) {
               if (response instanceof Object)
               {        
                   //Send clicked

                }
                else if (response === null)
                {
                    //Cancel clicked
                }
                else 
                    //X clicked
            };

Hope this helps! :)

like image 31
Lionz Avatar answered Sep 18 '22 03:09

Lionz