0

サンプルコードがあります:

<script>
window.fbAsyncInit = function() {
   // init the FB JS SDK
    FB.init({
        appId      : 'xxx', // App ID from the App Dashboard
        status     : true, // check the login status upon init?
        cookie     : true, // set sessions cookies to allow your server to access the session?
        xfbml      : true  // parse XFBML tags on this page?
    });

    FB.getLoginStatus(function(response) {
          var page_id = "40796308305"; // cocacola
          if (response && response.authResponse) {
            var user_id = response.authResponse.userID;
            var fql_query = "SELECT uid FROM page_fan WHERE page_id = "+page_id+"and uid="+user_id;
            FB.Data.query(fql_query).wait(function(rows) {
              if (rows.length == 1 && rows[0].uid == user_id) {
                $('#fbframe').css('display', 'none');
              } else {
                $('#fbframe').css('display', 'block');
              }
            });
          } else {
            alert("Login to like");
          }
    });   
}
(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

そしてhtml

<div id="fbframe">
   <fb:like href="https://www.facebook.com/coca-cola" send="false" width="450" show_faces="false"></fb:like>
</div>  

コードを実行すると、ページ facebook.com/coca-cola が気に入りました$('#fbframe').css('display', 'none');が、chrome では実行されません (firefox run OK)

4

1 に答える 1

0

私の推測では、jQuery ライブラリは最初に呼び出した時点ではまだロードされていません。

jQuery を使用する場合は、ドキュメントが最初に読み込まれていることを確認する必要があります。

次のように loginStatus 呼び出しをラップします。

$(document).ready(function() {
     FB.getLoginStatus(function(response) {
          var page_id = "40796308305"; // cocacola
          if (response && response.authResponse) {
            var user_id = response.authResponse.userID;
            var fql_query = "SELECT uid FROM page_fan WHERE page_id = "+page_id+"and uid="+user_id;
            FB.Data.query(fql_query).wait(function(rows) {
              if (rows.length == 1 && rows[0].uid == user_id) {
                $('#fbframe').css('display', 'none');
              } else {
                $('#fbframe').css('display', 'block');
              }
            });
          } else {
            alert("Login to like");
          }
    });   
});
于 2012-11-08T07:52:36.070 に答える