2012-06-12 22 views
1

我使用Facebook的javaSDK爲我的網站,我有一個奇怪的問題。 當我用「Internet Explorer」加載網站時,它總是運行正常。 當我用firefox加載網站時,它總是加載TWICE,而當我使用chrome時,JSDK有時會加載一次,有時根本不加載,給我錯誤「Uncaught ReferenceError:FB is not defined」。 我嘗試使用異步加載的SDK,但仍然是同樣的問題。 btw我有Firefox上的螢火蟲,這可能是JSDK加載兩次的原因嗎?如何修復Facebook 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,即時試圖integrete在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'; 
} 

},真);

感謝

+0

您是否有任何示例代碼顯示此問題? –

+0

添加示例代碼,當我嘗試使用FB.getLoginStatus時出現問題 – user1108310

回答

1

你必須調用FBLoginStatus()中,fbAsyncInit

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/

+0

謝謝!。我的代碼實際上是以fb.getlogin位於不同頁面的方式構建的,但我試圖將它們聯合起來。你有什麼想法爲什麼它在Firefox上加載兩次? – user1108310

+0

@ user1108310你需要添加一個頻道。 – Philip

1

When I load the website with firefox it always loads TWICE,

嘗試建立一個channel.html文件,並引用它的初始化。

and when I use chrome the JSDK sometimes loads once and sometimes not loads at all , giving me the error "Uncaught ReferenceError: FB is not defined ".

如果你不得不FB方法包裝成window.fbAsyncInit那是不可能的你的電話,因爲如果沒有被加載SDK該回調永遠不會被調用。結論:你沒有,但是你把你的FB.getLoginStatus調用放在其他地方(你還沒有在你的代碼中顯示我們)。

+0

謝謝!生病的fb.getlogin代碼,但我不認爲它的問題的來源,因爲它始終在InternetExplorer上工作。 – user1108310

+0

__Flawed__腳本邏輯沒有得到_right_只是因爲它可能在某些特定的瀏覽器中工作... – CBroe

+0

添加了FB.getlogin的腳本,請查看您是否可以在我的代碼中找到最新錯誤\: – user1108310