简体   繁体   English

Facebook Javascript SDK未加载

[英]Facebook Javascript SDK Not Loading

I have just started working with the Facebook SDK and I am trying to use a script which grabs a user's name and friend's list. 我刚刚开始使用Facebook SDK,我正在尝试使用一个抓取用户名和朋友列表的脚本。 This is the initialization code I am working with: 这是我正在使用的初始化代码:

window.fbAsyncInit = function()
{

    console.log("initialising");
    FB.init(
    {
      appId      : 'APPID',
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      oauth      : true, // enable OAuth 2.0
      xfbml      : false // dont parse XFBML
    });


    FB.getLoginStatus(function(response) {
        if (response.status === 'connected') {
            console.log("RESPONSE IS CONNECTED!");
        } else if (response.status === 'not_authorized') {
            console.log("NOT AUTHORIZED");
        } else {
            console.log("NOT LOGGED IN");
            FB.login();
        }
    });


};

(function(d, s, id){
 console.log("Loading SDK!");
 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'));

I have an fb-root element at the top of the document, outside of the script tags. 我在脚本标记之外的文档顶部有一个fb-root元素。 About half of the time, the SDK loads fine and I see the initialising and connected messages in the console. 大约一半的时间,SDK加载正常,我在控制台中看到初始化和连接的消息。 However, the other half of the time, I only see the "Loading SDK" message and nothing else, which I assume means that the SDK isn't loading correctly for whatever reason. 但是,另一半的时间,我只看到“正在加载SDK”消息而没有别的,我认为这意味着SDK无论出于何种原因都未正确加载。 I have also noticed that it loads on navigation to the page, but rarely on refresh. 我也注意到它加载到页面导航,但很少刷新。

My question is: what is causing the SDK to not load some of the time and how can I solve the issue? 我的问题是:什么导致SDK无法加载某些时间,我该如何解决问题?

According to the latest documentation from Facebook regarding the use of the Facebook JavaScript SDK, the method to include the SDK asynchronously has been updated: 根据Facebook关于使用Facebook JavaScript SDK的最新文档 ,异步包含SDK的方法已更新:

(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));

It's not immediately obvious to me why this would be more reliable as compared to your approach but I can say this is what I use on my site and I have not experienced any intermittent issues, 对于我来说,为什么与您的方法相比,这会更加可靠,但我可以说这是我在我的网站上使用的,而且我没有遇到任何间歇性问题,

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

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