2010-08-22 4 views
4

나는이 사용되었습니다에 따르면Facebook 자바 스크립트 기능이 준비되면 어떻게 감지합니까?

function FB_wait() { 
    if (typeof FB == 'undefined') { 
     window.setTimeout(FB_wait, 100); 
    } else { 
     more(); 
    } 
} 

를하지만 이건 내 초기화 코드 더 좋은 방법

window.fbAsyncInit = function() { 
    FB.init({ 
     appId : '<?php echo $conf['fb']['appid']; ?>', 
     status : true, // check login status 
     cookie : true, // enable cookies to allow the server to access the session 
     xfbml : true // parse XFBML 
    }); 
    FB.Canvas.setAutoResize(); 
    }; 

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

..

+1

FB 스크립트를 비동기 적으로로드합니까? – Anurag

+0

예 .. 코드를 포함 시켰습니다. – Pablo

답변

1

이 있어야 FaceBook JavaScript SDK Documentation 당신은 fbAsyncInit()를 작성해야 기능. FB이 준비가 될 것입니다 즉시 실행할 수 있습니다 :

var fbAsyncInit = function() { 
    // FB is ready 
}; 
+4

이것은 FB가 시작된 것을 의미하지 않습니다. FB 기능이 존재한다는 것입니다. 편집에 포함 된 시작 코드가 표시되면 실제로 제공 한 코드가 실제로 해당 코드의 일부라는 것을 알 수 있습니다. – Pablo

0

당신이 그것을주는 기능을 실행할 방법으로, 많은 jQuery의 준비 기능처럼 동작 window.fbReady() 함수를했다 FB 객체가로드되었거나 그렇지 않은 경우 대기열에 추가됩니다. 이 paramether으로 옵션의 단지 배열을 받아 봐 때문에

;(function(){ 
    // Function to have a list of functions to load on fbAsyncInit 
    var toLoad = [] 
    window.fbReady = function(func){ 
    if(typeof func === 'function') { 
     if(window.FB) { 
     func.call(window) 
     } else { 
     toLoad.push(func) 
     } 
    } 
    } 

    window.fbAsyncInit = function() { 
    FB.init({ 
     appId: FACEBOOK_API_KEY, 
     status: true, 
     cookie: true, 
     xfbml: true 
    }) 

    // Execute all the fbAsyncInit functions 
    toLoad.forEach(function(func){ 
     func.call(window) 
    }) 
    }; 
})(); 
(function(d, debug){ 
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; 
    if (d.getElementById(id)) {return;} 
    js = d.createElement('script'); js.id = id; js.async = true; 
    js.src = "//connect.facebook.net/en_US/all" + (debug ? "/debug" : "") + ".js"; 
    ref.parentNode.insertBefore(js, ref); 
}(document, /*debug*/ false)); 
0

당신은,)을 window.fbAsyncInit 내부에 FB 기능을 삽입 할 수 있지만 FB.init (투입한다. 코드는 다음과 같아야합니다.

window.fbAsyncInit = function() { 
    FB.init({ 
     appId : '<?php echo $conf['fb']['appid']; ?>', 
     status : true, // check login status 
     cookie : true, // enable cookies to allow the server to access the session 
     xfbml : true // parse XFBML 
    }); 

    // at this level, the FB library is ready 
    FB.Canvas.setAutoResize(); 
    FB.getLoginStatus(function(resp){ console.log(resp) }); 
    FB.any_fb_function(); 

    (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); 
    }()); 
}; 
관련 문제