Search code examples
facebookfacebook-graph-apifacebook-likefacebook-javascript-sdk

FB.init is called twice but I only have a like button from Facebook


I'm currently trying to allow user to connect to my site with their Facebook account but I have some issues.

First of all, you have to know that I already added a Like button with this code:

 <div id="fb-root"></div>
<script>(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/fr_FR/all.js#xfbml=1&appId=myappid";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

<script type="text/javascript">
    var urlfb = document.location.href;
     $(document).ready(function(){
        $('fb\\:like').attr('href', urlfb);
    });
</script>

So I saw the post of Facebook on how to add Facebook Login to his website but each time I launch the page where I pasted the FB code, there is this famous error message: FB.init has already been called - this could indicate a problem.

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
  FB.init({
    appId      : 'myappid', // App ID
    channelUrl : '', // Channel File
    status     : true, // check login status
    cookie     : true, // enable cookies to allow the server to access the session
    xfbml      : true  // parse XFBML
  });

What do I have to do to remove this error ? I tried to remove my Like button and no more error but I want to keep this like button, so what do I have to delete to make it works ?

Any ideas ?

Thanks


Solution

  • Adding #xfbml=1&appId=myappid to the end of the SDK URL makes it automatically initialze itself – so just remove that part, change

    js.src = "//connect.facebook.net/fr_FR/all.js#xfbml=1&appId=myappid";
    

    into

    js.src = "//connect.facebook.net/fr_FR/all.js";