最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript facebook check if user is fan of a page - Stack Overflow

programmeradmin6浏览0评论

This is my code:

    <div id="fb-root"></div>

<script>
  window.fbAsyncInit = function() {
    // init the FB JS SDK
    FB.init({
      appId      : '',                        // App ID from the app dashboard
      channelUrl : 'http://.php', // Channel file for x-domain ms
      status     : true,                                 // Check Facebook Login status
      xfbml      : true                                  // Look for social plugins on the page
    });

    // Additional initialization code such as adding Event Listeners goes here
    FB.api('me/likes/',function(response) {
        if( response.data ) {
            if( !isEmpty(response.data) )
                alert('You are a fan!');
            else
                alert('Not a fan!');
        } else {
            alert(response);
            alert('ERROR!');
        }
    });
  };

  // Load the SDK asynchronously
  (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/en_US/all.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));



// function to check for an empty object
function isEmpty(obj) {
    for(var prop in obj) {
        if(obj.hasOwnProperty(prop))
            return false;
    }

    return true;
}
</script>

But I have returned Error String... what I am doing wrong?

This is my code:

    <div id="fb-root"></div>

<script>
  window.fbAsyncInit = function() {
    // init the FB JS SDK
    FB.init({
      appId      : '',                        // App ID from the app dashboard
      channelUrl : 'http://.php', // Channel file for x-domain ms
      status     : true,                                 // Check Facebook Login status
      xfbml      : true                                  // Look for social plugins on the page
    });

    // Additional initialization code such as adding Event Listeners goes here
    FB.api('me/likes/',function(response) {
        if( response.data ) {
            if( !isEmpty(response.data) )
                alert('You are a fan!');
            else
                alert('Not a fan!');
        } else {
            alert(response);
            alert('ERROR!');
        }
    });
  };

  // Load the SDK asynchronously
  (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/en_US/all.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));



// function to check for an empty object
function isEmpty(obj) {
    for(var prop in obj) {
        if(obj.hasOwnProperty(prop))
            return false;
    }

    return true;
}
</script>

But I have returned Error String... what I am doing wrong?

Share Improve this question edited Apr 9, 2013 at 13:30 michele asked Apr 9, 2013 at 13:16 michelemichele 26.6k31 gold badges118 silver badges173 bronze badges 1
  • You are aware that you have to have the user connect to your app and grant user_likes permission before you can get this info, right …? – C3roe Commented Apr 9, 2013 at 13:24
Add a ment  | 

2 Answers 2

Reset to default 9

To get if the user is fan of the page or not you can do this :

FB.api({
    method:     'fql.query', 
    query:  'SELECT uid FROM page_fan WHERE uid=user_id AND page_id=page_id'
}, function(resp) {
    if (resp.length) {
        alert('A fan!')
    } else {
        alert('Not a fan!');
    }
}
);

You can read more about this here : http://www.masteringapi./tutorials/facebook-api-check-if-a-user-is-fan-of-a-facebook-page/20/

Similar Question :

Facebook Javascript, How to detect if user is a fan of my facebook page? While on my site?

I wasn't performing the login and token access! https://developers.facebook./docs/howtos/login/getting-started/

发布评论

评论列表(0)

  1. 暂无评论