如何修复有时加载两次、一次或根本不加载的 Facebook JSDK

how to fix Facebook JSDK that sometimes loads twice, once, or doesnt load at all?

本文关键字:加载 JSDK Facebook 一次 两次 何修复      更新时间:2023-09-26

我为我的网站使用facebook javaSDK,我有一个奇怪的问题。当我使用"IE浏览器"加载网站时,它总是运行良好。当我用 firefox 加载网站时,它总是加载两次,当我使用 chrome 时,JSDK 有时会加载一次,有时根本不加载,给我错误"未捕获的引用错误:FB 未定义"。我尝试对SDK使用异步加载,但仍然是相同的问题。顺便说一句,我在火狐上有火虫,这可能是 JSDK 加载两次的原因吗?

这是加载 JSSDK 的代码

<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
  appId      : '108437552626598', // App ID
  status     : true, // check login status
  cookie     : true, // enable cookies to allow the server to access the session
  xfbml      : true  // parse XFBML
});

};
 // 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>
我试图

使用FB.getlogin,我试图在Wordpress网站中集成facebook opengraph。加载完主要内容后立即退出我的脚本这是代码

             </div><!-- end #content -->

<script type="text/javascript"      src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
 <script type="text/javascript">
 FB.getLoginStatus(function(response) {
 if (response.status == 'connected') {
alert('in');
window.facebooklogin = 'true';
    window.myValue = response.authResponse.accessToken;  
window.myId = response.authResponse.userID;
$('.userimage img').attr('src','http://graph.facebook.com/' + window.myId +         '/picture');
 $.ajax({ url: 'http://ballvids.com/check/admin_checkuser.php',
     data: {action: window.myId},
 dataType:'json',
     type: 'post',
     success: function(output) {
    if (output.message == 'true'){
        $('.thumbnail   img').attr('src',"http://ballvids.com/images/on2.png");
        alert(window.myValue);
        FB.api('/me/video.watches?video=<?php echo   urlencode(get_permalink($post->ID));?>&access_token='+window.myValue,'post',function(response) {
             if (!response || response.error) {
            alert(response.error.message);
            } else {
            alert(response.id);
            postID=response.id;
            showStuff('removeTimeline');
            }  
                });
    }else{
        $('.thumbnail img').attr('src',"http://ballvids.com/images/off2.png");
    }
        }
});
}else{
    alert('inside2');
    $('.thumbnail img').attr('src',"http://ballvids.com/images/off2.png");
    window.facebooklogin = 'false';
}

},true);

谢谢

你必须在fbAsyncInit内调用FBLoginStatus()

window.fbAsyncInit = function() {
    FB.init({
        appId  : APP_ID,
        status : true,
        cookie : true,
        xfbml  : true
    });
    FB.getLoginStatus( function(response) {
        console.log(response);
    }, true);
};

如果从FB.getLoginStatus()返回的缓存对象出现问题,则必须传递第二个参数true

文档:https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/

当我用火狐加载网站时,它总是加载两次,

尝试设置通道.html文件并在初始化时引用它。

当我使用 chrome 时,JSDK 有时会加载一次,有时根本不加载,给我错误"未捕获的引用错误:FB 未定义"。

如果将对 FB 方法的调用打包到 window.fbAsyncInit 中,那是不可能的,因为如果未加载 SDK,则永远不会调用该回调。结论:你没有,而是把你的FB.getLoginStatus调用放在其他地方(你还没有在你的代码中向我们展示)。