fbAsyncInit中的“FB未定义”

时间:2013-01-29 23:02:44

标签: javascript facebook facebook-graph-api facebook-javascript-sdk

fbAsyncInit回调函数中的FB对象仍未定义。一旦FB加载并准备好使用FB.init({})进行初始化,这不是一个回调的目的吗?

如果我在setTimeout中添加另一个异步函数,则会加载FB对象。

请参阅此jsFiddle

显然我错过了一些东西。有人可以澄清吗?

2 个答案:

答案 0 :(得分:3)

这令人尴尬。你能看到丢失的分号吗?谢谢JSLint;

<div id="fb-root"></div>

<script>
    window.fbAsyncInit = function () {
    debugger;
       if (typeof FB !== "undefined") alert('FB loaded now');
       else alert('FB not loaded');

        //This works
        setTimeout(function () { 
                if (typeof FB !== "undefined") alert('FB loaded now');
               else alert('FB still not loaded');
            },100);
    }

    (function () {
        debugger;
        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);
    } ());

</script>

答案 1 :(得分:2)

<div id="fb-root"></div>

<script>
window.fbAsyncInit = function () 
{
   debugger;
   if (typeof FB !== "undefined") alert('FB loaded now');
   else alert('FB not loaded');

    //This works
    setTimeout(function () 
    { 
        if (typeof FB !== "undefined") 
            alert('FB loaded now');
        else 
            alert('FB still not loaded');
     },100);

};   // --> SEMI-COLON HERE 

(function () {
    debugger;
    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);
} ());

</script>