2014-03-13 57 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 }); 
    }); 
});