您只需要包含一次脚本引用并在页面加载时在脚本上调用init。您可以将其用于 fb:like 按钮或登录脚本。
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
FB.init({
appId : 'YOUR APP ID',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
</script>
关键是将 xfbml 设置为 true(这就是 #xfbml=1 的作用)。如果您没有该设置,则类似按钮将不会呈现。
如果您想订阅登录事件,只需在 FB.init() 调用之后添加以下脚本:
FB.Event.subscribe('auth.login', function(response) {
window.location.reload(); // or something else...
});
只有当用户点击链接、按钮等时才会触发您的登录脚本。
function doLogin() {
FB.login(function(response) {
if (response.session) {
if (response.perms) {
// user is logged in and granted some permissions.
// perms is a comma separated list of granted permissions
} else {
// user is logged in, but did not grant any permissions
}
} else {
// user is not logged in
}
}, {perms:'read_stream,publish_stream,offline_access'});
}