这个函数是如何自动执行的呢?
我有以下代码(这是 facebook API 的初始化), 我不明白为什么这个代码片段在加载时运行而不在代码中的其他任何地方被调用!
有趣的是,当我删除 fbAsyncInit 函数
“包装器”时,它的工作原理是一样的! 那么如何在不调用其他地方的 fbAsyncInit
的情况下调用 FB.init
呢?
window.fbAsyncInit = function() {
FB.init({appId: '00000000000000000', status: true, cookie: true, xfbml: true});
FB.getLoginStatus(function(response) {
if (response.session) {
console.log('User is logged in.');
FB.api("/me?fields=name,picture", handleMe);
}
else {
console.log('User is not logged in.');
window.location = "http://wall-et.com/index.php/test/login/";
}
});
};
I have the following code (which is the initialization of the facebook API),
I don't understand why this snippet runs on-load without being called anywhere else in the code!
The funny thing is that when i remove the fbAsyncInit function
"wrapper" it works the same!
so how does the FB.init
is called without calling fbAsyncInit
anywhere else?
window.fbAsyncInit = function() {
FB.init({appId: '00000000000000000', status: true, cookie: true, xfbml: true});
FB.getLoginStatus(function(response) {
if (response.session) {
console.log('User is logged in.');
FB.api("/me?fields=name,picture", handleMe);
}
else {
console.log('User is not logged in.');
window.location = "http://wall-et.com/index.php/test/login/";
}
});
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此,您需要包含来自 Facebook 的脚本(可能是 connect.facebook.net/en_US/ all.js)在您的页面中。
该脚本从
window.setTimeout(..., 0)
运行window.fbAsyncInit
,导致您的代码在 Facebook 脚本完全加载和初始化后运行。For this to work, you need to include a script from Facebook (perhaps connect.facebook.net/en_US/all.js) in your page.
The script runs
window.fbAsyncInit
from awindow.setTimeout(..., 0)
, causing your code to be run after the Facebook script has been completely loaded and initialized.