Facebook的FB.init完成后如何检测 [英] How to detect when facebook's FB.init is complete
问题描述
我将其包含在每个页面的顶部:
< div id =fb-root>< / div>
< script>
window.fbAsyncInit = function(){
FB.init({
appId:'<?php echo $ conf ['fb'] ['appid'];?& ,
status:true,//检查登录状态
cookie:true,//启用Cookie以允许服务器访问会话
xfbml:true //解析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);
}());
< / script>
2012年1月4日更新
似乎你不能只调用依赖FB的方法(例如 FB.getAuthResponse()
)在 FB.init()
之前就像以前一样,因为 FB.init()
似乎是异步的。将代码包裹到 FB.getLoginStatus()
响应似乎是在检测API完全准备就绪时的诀窍:
window.fbAsyncInit = function(){
FB.init({
// ...
});
FB.getLoginStatus(function(response){
runFbInitCriticalCode();
});
};
或者如果使用 fbEnsureInit()
以下:
window.fbAsyncInit = function(){
FB.init({
//。 ..
});
FB.getLoginStatus(function(response){
fbApiInit = true;
});
};
原始帖子如果你想在FB初始化时运行一些脚本,你可以在 fbAsyncInit
中放置一些回调函数:
window.fbAsyncInit = function(){
FB.init({
appId:'& php echo $ conf ['fb'] ['appid'];?>',
status:true,//检查登录状态
cookie:true,//启用Cookie以允许服务器访问会话
xfbml:true //解析XFBML
});
FB.Canvas.setAutoResize();
runFbInitCriticalCode(); //包含FB init关键代码的函数
};
如果你想要确切地更换FB.ensureInit,那么你必须自己写一些东西,因为那里没有官方的替换(大错误imo)。这是我使用的:
window.fbAsyncInit = function(){
FB.init({
appId:'<?php echo $ conf ['fb'] ['appid'];?>',
status:true,//检查登录状态
cookie:true,//启用Cookie以允许服务器访问会话
xfbml:true //解析XFBML
});
FB.Canvas.setAutoResize();
fbApiInit = true; // init flag
};
函数fbEnsureInit(callback){
if(!window.fbApiInit){
setTimeout(function(){fbEnsureInit(callback);},50);
} else {
if(callback){
callback();
}
}
}
用法:
fbEnsureInit(function(){
console.log(一旦FB被初始化就运行);
});
The old JS SDK had a function called FB.ensureInit. The new SDK does not seem to have such function... how can I ensure that I do not make api calls until it is fully initiated?
I include this in the top of every page:
<div id="fb-root"></div>
<script>
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);
}());
</script>
Update on Jan 04, 2012
It seems like you can't just call FB-dependent methods (for example FB.getAuthResponse()
) right after FB.init()
like before, as FB.init()
seems to be asynchronous now. Wrapping your code into FB.getLoginStatus()
response seems to do the trick of detecting when API is fully ready:
window.fbAsyncInit = function() {
FB.init({
//...
});
FB.getLoginStatus(function(response){
runFbInitCriticalCode();
});
};
or if using fbEnsureInit()
implementation from below:
window.fbAsyncInit = function() {
FB.init({
//...
});
FB.getLoginStatus(function(response){
fbApiInit = true;
});
};
Original Post:
If you want to just run some script when FB is initialized you can put some callback function inside fbAsyncInit
:
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();
runFbInitCriticalCode(); //function that contains FB init critical code
};
If you want exact replacement of FB.ensureInit then you would have to write something on your own as there is no official replacement (big mistake imo). Here is what I use:
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();
fbApiInit = true; //init flag
};
function fbEnsureInit(callback) {
if(!window.fbApiInit) {
setTimeout(function() {fbEnsureInit(callback);}, 50);
} else {
if(callback) {
callback();
}
}
}
Usage:
fbEnsureInit(function() {
console.log("this will be run once FB is initialized");
});
这篇关于Facebook的FB.init完成后如何检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!