gpt4 book ai didi

javascript - 为什么 Facebook Javascript SDK 是异步加载的?

转载 作者:数据小太阳 更新时间:2023-10-29 04:17:17 25 4
gpt4 key购买 nike

我在我的 Facebook 应用程序中使用标准的 Facebook Javascript 代码,这是我在 Heroku 上的示例应用程序中获得的。我不太了解它,但通过细心,我已经能够修改示例应用程序来执行一些对我有用的基本操作。

我的理解是这段代码以某种方式加载了 Facebook Javascript SDK。也就是说,它加载了一个 javascript 代码文件。它真正加载的是什么文件?为什么我不能像加载任何其他 javascript 代码文件一样加载它?例如:

<script type="text/javascript" src="/javascript/jquery-1.7.1.min.js"></script>

这种高度复杂的方法的缺点是我无法弄清楚它的正反面,而且因为我不理解它,所以我对如何使用它没有直觉。

代码在这里:

<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo AppInfo::appID(); ?>', // App ID
channelUrl : '//<?php echo $_SERVER["HTTP_HOST"]; ?>/channel.html', // Channel File
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});

// Listen to the auth.login which will be called when the user logs in
// using the Login button
FB.Event.subscribe('auth.login', function(response) {
// We want to reload the page now so PHP can read the cookie that the
// Javascript SDK sat. But we don't want to use
// window.location.reload() because if this is in a canvas there was a
// post made to this page and a reload will trigger a message to the
// user asking if they want to send data again.
window.location = window.location;
});

FB.Canvas.setAutoGrow();
};

// Load the SDK Asynchronously
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>

最佳答案

它使您的网站加载速度更快,因为它不需要等待来自 Facebook 服务器的响应。

window.fbAsyncInit 是一个回调,在加载脚本时执行。所以当 facebook 服务器宕机时,这个函数永远不会被调用。

加载 api 后,API 的所有功能都可用。只需检查文档。 http://developers.facebook.com/docs/reference/javascript/

希望这能澄清。

关于javascript - 为什么 Facebook Javascript SDK 是异步加载的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15020011/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com