I was wondering if anyone could help clear up this issue I am having.
I have been trying all morning to get a Facebook share button to work with dynamic parameters unique to the video that was being played on the page at the time.
I think the following should work but I am getting the error: share_title is not defined.
I then tried setting the contents of a div to the contents of the ajax response and then setting the params variable to the contents of that div so that the variables would be accessible within the FB.ui function but that didn’t seem to work either.
Does anyone have any ideas?
$('.share_button').live('click', function(e){
$('#player').fadeOut('slow');
var share_id = $(this).attr('id');
$.ajax({
type: 'GET',
url: '/youtube.php',
data: 'share='+ share_id,
success: function(data) {
//$('#params').html(data);
//params = $('#params').html();
param = data.split('--');
share_title = param[0];
share_description = param[1];
share_picture = param[2];
share_views = param[3];
}
});
e.preventDefault();
FB.ui(
{
method: 'feed',
name: share_title,
link: 'http://www.facebook.com',
picture: share_picture,
caption: share_views,
description: share_description
},
function(response) {
$('#player').show('slow');
});
});
You need to put the
FB.uicall in the AJAX callback.AJAX is asynchronous. The call to
$.ajaxreturns before the HTTP request has completed (i.e. before thesuccesscallback has executed), and the program flow continues. This means that, in this situation;$.ajaxe.preventDefault()FB.uishare_titleis populated.FYI, you should also declare you’re variables; otherwise you’re making them implicit globals, which are bad;