2014-03-13 128 views
5

我想使用下面的代码,我基于新的文档显示“post to feed”对话框(https://developers.facebook.com/docs/javascript/reference/FB.ui),但我得到以下错误“ReferenceError:FB不是定义Facebook共享通过FB.ui

,我使用的代码是最简单的我能想出:

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.' 
    }); 

任何想法?

编辑1

如果我想要在用户点击一个链接,我会使用jQuery的单击事件

$(".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.' 
    }); 
}); 

或有FB.ui函数内部以打开对话框,接受参数并调用这个函数

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')); 

,并在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.') 
} 

回答

7

好地方,有你这样做有几个概念错误。

第一:

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.' 
}); 

这是什么应该做的?无需用户交互打开共享对话框?当Share对话框应该出现时,必须调用FB.ui,而不是在Facebook AsyncInit函数之后。

而且,facebook sdk将以异步方式启动,正如函数的名称所示。这意味着FB将不会在您放置该功能的地方定义。

二:

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功能,在这种情况下,是fbAsyncInit函数内的本地功能。因此,您无法访问fbAsyncInit之外的FB_post_feed函数。

此外,FB.init是异步的,这意味着在创建FB_post_feed时FB将不确定。

根据您的HTML代码是如何定义的,如果这个标识符是正确的,代码

$(".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.' 
    }); 
}); 

应该正常工作。然而,只是一个建议:类通常用于设置样式。如果您使用按钮(或“a”元素)id代替,这将是最佳做法。

0

我有同样的问题,并通过请求由jQuery的Facebook的脚本,然后初始化它解决了这个问题:

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


$(document).ready(function() 
{ 
    $.getScript("http://connect.facebook.net/en_US/all.js#xfbml=1", function() { 
     FB.init({ appId: 'xxxxxxxx', status: true, cookie: true, xfbml: true }); 
    }); 
});