RSS
 

Solution for Facebook Error “FB.login() called before calling FB.init().”

01 Feb

Just thought I’d share this in case anyone comes googling, as I could not find a satisfactory search result, but was fortunate that it was obvious enough that I realized the issue.

I had a page that has had a FB “like” button forever with this code:

<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script> 
<fb:like layout="button_count" show_faces="true" width="80" font="verdana"></fb:like>


I just finished using a bit of the new Facebook API to add some Facebook Connect functionality, including a “Login with Facebook” button in the sidebar of all pages in addition to the Facebook JavaScript SDK asynchronous loader:

<script type="text/javascript">// <![CDATA[
  window.fbAsyncInit = function(){
	FB.init({
	  appId   : '...',
	  session : ...,
	  status  : true,
	  cookie  : true,
	  xfbml   : true
	});

	FB.Event.subscribe('auth.login', function(){
	  window.location.reload();
	});
  };
  (function(){
	var e = document.createElement('script');
	e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
	e.async = true;
	document.getElementById('fb-root').appendChild(e);
  }());
// ]]></script>


Before too much time passed, I discovered that on just some pages, one or both of these things would occur:

  1. Trying to click on the “Login with Facebook” button would do nothing but result in this JS console error: “FB.login() called before calling FB.init().”
  2. The page would just keep reloading itself.



You may have guessed just by looking at the above code what the cause of the problem is. The pre-existing “like” button code is loading the all.js script synchronously, but the Facebook Connect code is also loading the same file, but asynchronously. The fix is even more obvious: Remove the old <script> tag that is paired with the like button. Et voila, fin.

 
 

72,024 views

Tags:

Leave a Reply

 

 
  1. Caio

    February 13, 2011 at 7:24 am

    Fantastic. I was getting the same trouble and could’t find any solution. But taht worked fine.

    Thanks a lot.

     
  2. Rafal

    March 1, 2011 at 6:13 am

    Thanks a lot! That was it

     
  3. leon

    May 6, 2011 at 5:45 pm

    thank you !!!!!!!!!!!!!

     
  4. Qutaiba

    June 21, 2011 at 6:38 pm

    Thanks, this saved my life

     
  5. Look

    October 6, 2011 at 3:41 pm

    Great !!!….
    Saved our lives here..

     
  6. Artemis Wolf

    April 18, 2012 at 4:33 am

    Mine just won’t let me enter Facebook. It’ll look like its gonna load but then “error please try again later” will appear. when I click ok it takes me to the Newsfeed but won’t let me post anything or click on somebody elses post.

     
  7. oOLokiOo

    June 4, 2012 at 4:26 am

    Thank’s a lot, it works! :)

     
  8. Steve

    August 25, 2012 at 6:34 am

    Thank you Sir, this problem was driving me mental!!

    The addition of FB like to an existing page was the culprit in my case.

    Thank god I found your post.