e3matheus
e3matheus

Reputation: 2132

How to check if facebook fan page exists with javascript?

In my app, I have a facebook field that can refer to a facebook user or to a facebook user fanpage.

I want to render the facebook fan page like box only if the fanpage exists in Facebook. Is there a way to do this with the Javascript SDK?

Upvotes: 2

Views: 2054

Answers (3)

owyongsk
owyongsk

Reputation: 2469

If you don't want to load the FB SDK, you can do an AJAX get request to eg. http://graph.facebook.com/facebook. This works with the cross origin policy because graph.facebook.com allows different domains.

The response is in JSON so something like this would work:

function startCheckForFanPage(username) {
    $.get("graph.facebook.com/" + username, function(response) {
        if (response)
        // otherwise, do Y
    }, function() {
        // Handle 404 or other failures here
    })
}

EDIT: This no longer works

Upvotes: 1

if you use the Facebook JS SDK you can do this after initializing the FB object

    FB.api('/the_fanpage_path', function(response) {
      if(response.is_published) {
        // it is a Fan Page!
      }
    });

Upvotes: 2

machineghost
machineghost

Reputation: 35820

If you use jQuery and can figure out the pattern for the URL of those pages, you can use jQuery.get to check whether they exist or not. If you don't use jQuery, you can do the same thing with raw XmlHttpRequests.

For example, if the URL is "www.facebook.com/fanpage/{{username}}", you can do:

function startCheckForFanPage(username) {
    $.get("www.facebook.com/fanpage/" + username, function(response) {
        // if response.showsThere'sAPageThere, do X
        // otherwise, do Y
    }
}

Upvotes: 3

Related Questions