繁体   English   中英

为什么要异步加载Facebook Javascript SDK?

[英]Why is Facebook Javascript SDK loaded asynchronously?

我在我的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/

希望这可以澄清。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM