1

ユーザーが私のindex.htmlページのリンクをクリックした後、facebook javascript sdkを初期化し、ログインステータスを確認したいと考えています。何らかの理由で、これは機能しません。jQueryは機能しており、initコードを個別にテストすると機能しますが、まとめると機能しなくなります。理由はありますか?

これは私の index.html ページです:

<!DOCTYPE html>
<html>
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

</head>
<body>
<div id="fb-root"></div>
<script>

// この jquery は init プロセスをトリガーするはずです

$('document').ready(function(){
          $('a').click(function(){
             alert("this works");

  // Additional JS functions here
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '39672*******', // App ID
      channelUrl : '//http://spilot.koding.com/channel.html', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });

    // get login status
FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    // connected: redirect to start scene page
    alert("testing");
  } else if (response.status === 'not_authorized') {
    // not_authorized
    login();
  } else {
    // not_logged_in
    login();
  }
 });
  };

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

//ログインボックスを表示

   function login() {
    FB.login(function(response) {
        if (response.authResponse) {
            // connected
            alert("connected");
        } else {
            // cancelled
        }
    });
}
});
});


</script>





<div id="findScene"><a href=""><h1>Find</h1></a></div>
<div id="startScene"><a href=""><h1>Host</h1></a></div> 
</body>

</html>
4

1 に答える 1

2

JS SDK は非同期的に読み込まれます。ページ全体や残りのリソースが完了する前に完了する可能性があります。

そのため、関数を に渡すのはwindow.fbAsyncInitすでに遅いかもしれません$('document').ready

window.fbAsyncInitハンドラー関数を関数の外側に割り当てます$('document').ready

于 2013-03-06T18:30:09.347 に答える