javascript - 有没有办法检测 Facebook Javascript SDK 是否加载成功?

标签 javascript facebook sdk

我使用 Facebook 作为我网站的成员(member)系统。它使用代码生成登录控件,允许用户通过他们的 facebook 帐户登录。如果他们已经是成员(member),则基本上只需单击一次,如果不是,则单击 2 次(用于授予权限)。

不过我遇到了一个问题...反馈表明登录按钮并不总是正确加载。它没有加载 facebook 登录控件,而是简单地(以文本形式)声明“通过 facebook 登录”——如果控件成功加载,登录按钮将显示此内容。

测试表明,当 facebook javascript SDK 无法完全加载(无论出于何种原因)时,就会发生这种情况。我见过 url 中的 # 阻止 SDK 加载的情况。

为了更好地支持这个问题,我将如何检测 facebook javascript SDK 是否已加载并准备就绪?这样,如果它失败了,我可以为用户留下一些注释。

这是它当前添加到页面的方式:

<script>
window.fbAsyncInit = function () {
FB.init({
  appId: '***************',
  status: true,
  cookie: true,
  xfbml: true
});
FB.Event.subscribe('auth.login', function (response) {
  window.location.reload();
});

};
(function () {
  var e = document.createElement('script'); e.async = true;
  e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
  document.getElementById('fb-root').appendChild(e);
} ());

</script>

最佳答案

您应该加载 Javascript Library Asynchronously并将所有与 FB 相关的函数放入 window.fbAsyncInit 方法中:

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'YOUR_APP_ID', // App ID
      channelUrl : '//WWW.YOUR_DOMAIN.COM/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
    });

    // Additional initialization code here
  };

  // 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));
</script>

This code loads the SDK asynchronously so it does not block loading other elements of your page. This is particularly important to ensure fast page loads for users and SEO robots.

The URLs in the above code are protocol relative. This lets the browser to load the SDK over the same protocol (HTTP or HTTPS) as the containing page, which will prevent "Insecure Content" warnings.

The function assigned to window.fbAsyncInit is run as soon as the SDK is loaded. Any code that you want to run after the SDK is loaded should be placed within this function and after the call to FB.init. For example, this is where you would test the logged in status of the user or subscribe to any Facebook events in which your application is interested.

一个简单的例子如下:

<div id="fb-root"></div>
<script>
  var isLoaded = false;
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'YOUR_APP_ID', // App ID
      channelUrl : '//WWW.YOUR_DOMAIN.COM/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
    });
    isLoaded = true;

    // Additional initialization code here
  };

  function checkIfLoaded() {
    if(isLoaded) console.log("LOADED!");
    else console.log("NOT YET!");

    return false;
  }

  // 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));
</script>
<a href="#" onclick="checkIfLoaded();">Check</a>

enter image description here
(只需点击几次 check 链接)


请注意,您仍然可以在没有 JavaScript 的情况下在服务器端构建登录链接。使用 PHP-SDK 的示例:

$loginUrl = $facebook->getLoginUrl();
...
...
<a href="<?php echo $loginUrl; ?>">
    <img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">
</a>

关于javascript - 有没有办法检测 Facebook Javascript SDK 是否加载成功?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5334977/

相关文章:

安卓 SQL 错误

objective-c - 如何使用 SDK 3.1 指定用户用于发布的应用程序名称(通过应用程序名称)

facebook - Firebase 实时数据库下载使用率高

javascript - 从 firefox 扩展打开新窗口被拒绝访问

JavaScript 函数验证器

由于默认浏览器,Android 无法在 webview 中实现 facebook 评论

docker - 无法在 docker 镜像中使用 sdkman 安装 groovy

iphone - 我可以有一个 UICollectionViewFlowLayout 与布局方向不同的 scrollDirection 吗?

javascript - 正则表达式匹配 Richtext 中的 uri 模式

javascript - 单击了哪个 <li> 元素?