2011-10-16 32 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块。