Question: Seeing "Uncaught ReferenceError: FB is not defined in console"

Question

Seeing "Uncaught ReferenceError: FB is not defined in console"

Answers 1
Added at 2016-12-30 02:12
Tags
Question

In a process of hiding piece of content for people who didn't like our page yet. Using Facebook SDK for the first time. The idea is to have a call to action text and button. Once button is clicked, if user already likes our page- it shows a piece of content. If not- shows a like button and shows piece of content after it's clicked.

Here is connecting SDK

window.fbAsyncInit = function() {
FB.init({
  appId      : '348511868693215',
  xfbml      : true,
  version    : 'v2.8'
});
FB.AppEvents.logPageView();
};

   (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/sdk.js";
     fjs.parentNode.insertBefore(js, fjs);
   }(document, 'script', 'facebook-jssdk'));

And here is actual code for checking

   FB.api({
    method:     "pages.isFan",
    page_id:        page_id,
    },  function(response) {
        console.log(response);
        if(response){
            //if liked
        $(".show-poll").click(function(){
        $(".actualpoll").show();
        });
        } else {
            $(".show-poll").click(function(){
        $(".likepage").show();
        });
        }
    }
    );
Answers
nr: #1 dodano: 2016-12-30 02:12

Your code "for checking" is executed before the SDK is loaded, i.e. before FB is available. Move the code inside the window.fbAsyncInit function. That function is called once the SDK is available.

Source Show
◀ Wstecz