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

Mat*_*nes 8 javascript sdk facebook asynchronous

我在我的Facebook应用程序中使用标准的Facebook Javascript代码,这是我在Heroku上的示例应用程序中获得的.我真的不明白它,但是通过小心我已经能够修改示例应用程序来做一些对我有用的基本事情.

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

<script type="text/javascript" src="/javascript/jquery-1.7.1.min.js"></script>
Run Code Online (Sandbox Code Playgroud)

这种高度复杂的方法的缺点是我无法做出正面或反面,因为我不理解它,我对如何使用它没有直觉.

代码在这里:

<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>
Run Code Online (Sandbox Code Playgroud)

小智 4

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

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

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

希望这能澄清。

  • 为什么 Facebook Javascript SDK 需要使用这种技术,而几乎没有用于任何其他 Javascript 代码? (4认同)