hyperrjas
hyperrjas

Reputation: 10744

FB.init has already been called - this could indicate

I have a facebook button no SDK:

<fb:like href="<%= @canonical_url %>" send="" layout="button_count"></fb:like>
     <div id="fb-root"> </div>
     <script>
      // facebook recommend button
      window.fbAsyncInit = function() {
         FB.init({appId: 'myappid', status: true, cookie: true, xfbml: true});
      };
      (function() {
        // delay to simulate slow loading of Facebook library - remove this setTimeout!!
        var t = setTimeout(function() {
            var e = document.createElement('script'); e.async = true;
            e.src = document.location.protocol + '//connect.facebook.net/<%= locale_og_tag %>/all.js';
            document.getElementById('fb-root').appendChild(e);
        }, 0);
      }());
      </script>
  </div>

I don't reload facebook button when I change of page, then inside my javascript file I run:

if (typeof (FB) != 'undefined') {
    FB.init({ status: true, cookie: true, xfbml: true });
   } else {
    $.getScript("http://connect.facebook.net/en_US/all.js#xfbml=1", function () {
     FB.init({ status: true, cookie: true, xfbml: true });
   });
 }

but I get in console:

FB.init has already been called - this could indicate a problem

How can I fix this problem?

Thank you!

Upvotes: 1

Views: 10197

Answers (5)

Marek Wysmułek
Marek Wysmułek

Reputation: 961

Maybe it will help to others who - like me - has title like problem, and didn't find a solution.

The documentation of Facebook is missleading, giving this example:


      <script crossorigin="anonymous" src="https://connect.facebook.net/pl_PL/sdk/debug.js#xfbml=1&version=v7.0&appId={your app id}&autoLogAppEvents=1"></script>

      <script>
      window.fbAsyncInit = function() {
         FB.init({
            appId      : '{your app id}',
            cookie     : true,
            xfbml      : true,
            version    : 'v7.0'
         });

         FB.AppEvents.logPageView();   

         FB.getLoginStatus(function(response) {
            console.log(response);
         });

      };

      </script>


BUT it is wrong, because in url of the facebook script, one already does init the FB object (giving app id).

Thats why one need to choose the method:

  • or include facebook script WITHOUT giving app id, and then manifestly CALL FB.init(..)
  • or include facebook script WITH app id and then OMIT calling FB.init(...)

Upvotes: 0

user3706926
user3706926

Reputation: 181

Just separate the login form with other pages. So you don't load FB.init() many times.

Upvotes: 0

Sean Kinsey
Sean Kinsey

Reputation: 38046

You have three calls to init - two explicit calls through FB.init, and one implicit call via ../all.js#xfbml=1. Remove the #xfbml=1 part as well as either of the FB.inits and you should be good.

Upvotes: 15

Lix
Lix

Reputation: 47986

Well you are calling FB.init() twice... You might want to heed the advice of that error message :P

You only need to call it once and you couldn't call it without having the SDK loaded so, you do have to have the SDK. You call FB.init() to set parameters such as the app_id, whether to render xfbml durring initialization, etc...

Upvotes: 2

Fisch
Fisch

Reputation: 3815

You are already calling FB.init with window.fbAsyncInit and you are already loading the FB script in your first code block.

This means that you should remove the second code block and you should be fine.

Upvotes: 1

Related Questions