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 运行正常)

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 回答