我一直在分析Facebook JS SDK调试源代码,并且使用快速入门Facebook JS SDK代码进行一些测试,我发现FB.init函数是在fbAsyncInit函数中声明的,即使异步加载脚本仍未加载(?)。
我已从以下代码中解释了这一点:
window.fbAsyncInit = function() {
FB.init({
appId : '{your-app-id}',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
}
// Load the SDK asynchronously
(function(d){
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.js";
ref.parentNode.insertBefore(js, ref);
}(document));
任何人都可以解释这里发生了什么?
感谢。
答案 0 :(得分:0)
你的问题不是很清楚,但我会尝试解释这个问题 -
代码 -
function(d){
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.js";
ref.parentNode.insertBefore(js, ref);
}(document));
异步加载facebook的js sdk。现在以下代码 -
window.fbAsyncInit = function() {
FB.init({
appId : '{your-app-id}',
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
}
与文件中包含的内容无关,因为window.fbAsyncInit
只会在加载SDK后自动触发。触发此操作后,FB
已成功初始化!就是这样。希望有所帮助!