2011-10-16 66 views
1

我真的不理解這一切。我的目標是在我的主要aspx文件中沒有一堆js。我想在外部文件中。我根本無法讓它工作。更多麻煩facebook連接登錄(JS SDK)

以下似乎工作(得到登錄彈出),但它永遠不會更新「客人」的東西,登錄按鈕保持在那裏。控制檯說「FB沒有定義」,所以顯然它不知道FB.api意味着...對不起,我吸收這個:|

[-----的Index.aspx ----------------]

<%@ Page Language="vb" AutoEventWireup="false" CodeBehind="index.aspx.vb" Inherits="MyGameEngineBeta.index" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://ogp.me/ns/fb#"> 
    <head runat="server"> 
     <title>Scott's Test</title> 
     <script src="JS/jquery-1.6.4.min.js" type="text/javascript"></script> 
    </head> 
    <body> 
     <div id="fb-root"></div> 
     <script src="JS/facebook.js" type="text/javascript"></script> 
     <fb:login-button show-faces="false" width="200" max-rows="1"></fb:login-button> 
     <div>Welcome <span id="guest" class="UserNameWelcome">Guest</span><span id="name" class="UserNameWelcome"></span>!</div> 
    </body> 
    </html> 

[----- facebook.js- ---------------]

window.fbAsyncInit = function() { 
    FB.init({ 
     appId: 'mysiteid', // App ID 
     channelURL: '//www.mysite.net/channel.html', // Channel File 
     status: true, // check login status 
     cookie: true, // enable cookies to allow the server to access the session 
     oauth: true, // enable OAuth 2.0 
     xfbml: true // parse XFBML 
    }); 

    // Additional initialization code here 
    alert('Facebook initialized'); 
}; 

// Load the SDK Asynchronously 
(function (d) { 
    var js, id = 'facebook-jssdk'; if (d.getElementById(id)) { return; } 
    js = d.createElement('script'); js.id = id; js.async = true; 
    js.src = "//connect.facebook.net/en_US/all.js"; 
    d.getElementsByTagName('head')[0].appendChild(js); 
} (document)); 

//update guest/username if logged in or not 
FB.api('/me', function (user) { 
    if (user != null) { 
     alert('logged in'); 
     var name = document.getElementById('name'); 
     name.innerHTML = user.name 
     $('#guest').hide(); 
    } else { 
     alert('guest'); 
     $('#image').hide(); 
     $('#name').hide(); 
    } 
}); 

回答

2

你有window.fbAsyncInit處理程序,這是很好的內部調用FB.init,這樣,它不會得到執行,直到所有FB的東西加載。但是你可以在處理程序之外調用FB.api,這意味着你不能保證all.js在調用FB.api之前完成加載。最簡單的解決方案是僅在警報之後移動window.fbAsyncInit函數中的FB.api塊。