2013-08-24 63 views
0

我無法弄清楚如何成功加載和執行fb.ui函數。我看到你需要有一個Facebook應用程序ID,所以我創建了一個空白的應用程序,並把它放在appId部分。這是我當前的代碼:使用fb.ui創建共享彈出框

加載和初始化:

<div id="fb-root"></div> 
<script> 
    window.fbAsyncInit = function() { 

    FB.init({ 
     appId  : '1404387053111932', 
     channelUrl : 'channel.htm;', 
     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 = "//connect.facebook.net/en_US/all.js"; 
    fjs.parentNode.insertBefore(js, fjs); 
    }(document, 'script', 'facebook-jssdk')); 
</script> 

FB.ui腳本:

<script> 
FB.ui(
    { 
    method: 'feed', 
    name: 'The Facebook SDK for Javascript', 
    caption: 'Bringing Facebook to the desktop and mobile web', 
    description: (
     'A small JavaScript library that allows you to harness ' + 
     'the power of Facebook, bringing the user\'s identity, ' + 
     'social graph and distribution power to your site.' 
    ), 
    link: 'https://developers.facebook.com/docs/reference/javascript/', 
    picture: 'http://www.fbrell.com/public/f8.jpg' 
    }, 
    function(response) { 
    if (response && response.post_id) { 
     alert('Post was published.'); 
    } else { 
     alert('Post was not published.'); 
    } 
    } 
); 
</script> 

channel.html文件:

<html> 
    <head> 
    <script src="http://connect.facebook.net/en_US/all.js"></script> 
    </head> 

    <body> 

    </body> 

</html> 

我想我的主要問題我如何調用這段代碼來執行點擊操作?我是否會圍繞fb.ui {}包裝一個函數?

回答

0

是的,你可以將它包裝在一個函數中並調用onClick函數。只需調用FB.init部件即可。 以下是我用於發送請求的示例代碼。

FB.init({ 
    appId: '*****************', 
    status: true, 
    cookie: true, 
    xfbml: true 
}); 

function sendRequests() { 
    FB.ui({ 
     method: 'apprequests', 
     message: 'Lets play a game.', 
    }, function(response) { 
    }); 
} 

<a href="#" onclick="sendRequests();return false;">Invite your friends</a>