任何想法为什么这个Facebook连接脚本不起作用?

时间:2011-08-04 08:25:58

标签: javascript facebook

我想将此脚本附加到自定义的Facebook图标。我有这个javascript:

var fbClicked = false;
FB.init({appId: 'Myappid', status: true, cookie: true, xfbml: true});
function thFBShortCut() {
    FB.login(function(response) {
        fbClicked = true;
        if (response.session && response.perms && fbClicked) {
            window.location = 'http://www.mysite.com/facebookauth/?ref=/';
        }
    }, {perms:'email,publish_stream'});
}
function thFBLogin() {
    fbClicked = true;
    $('.popbox').fadeOut(); 
    FB.getLoginStatus(function(response) {
        if(response.session && response.status == 'connected' && fbClicked) {
            window.location = 'http://www.mysite.com/facebookauth/?ref=/';
        }
    });
}
FB.Event.subscribe('auth.login', function(response) {
    if(response.session && fbClicked){
        window.location = 'http://www.mysite.com/facebookauth/?ref=/';
    }
});

</script>

my html:
<a id="facebookicon" onclick="thFBShortCut()"</a>
maby,有可能以某种方式简化wthis脚本吗?

1 个答案:

答案 0 :(得分:0)

你只需要在FB.init();

之前加载Facebook javascript SDK

<body>

之后为SDK添加Facebook div
<div id="fb-root"></div>

和javascript:

(function() {
  var e = document.createElement('script');
  e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
  e.async = true;
  document.getElementById('fb-root').appendChild(e);
})();
window.fbAsyncInit = function() {
    var fbClicked = false;
    FB.init({appId: 'Myappid', status: true, cookie: true, xfbml: true});

    FB.Event.subscribe('auth.login', function(response) {
        if(response.session && fbClicked){
            window.location = 'http://www.mysite.com/facebookauth/?ref=/';
        }
    });
}
function thFBShortCut() {
    FB.login(function(response) {
        fbClicked = true;
        if (response.session && response.perms && fbClicked) {
            window.location = 'http://www.mysite.com/facebookauth/?ref=/';
        }
    }, {perms:'email,publish_stream'});
}
function thFBLogin() {
    fbClicked = true;
    $('.popbox').fadeOut(); 
    FB.getLoginStatus(function(response) {
        if(response.session && response.status == 'connected' && fbClicked) {
            window.location = 'http://www.mysite.com/facebookauth/?ref=/';
        }
    });
}