Uncaught ReferenceError: FB is not defined

笑着哭i 提交于 2020-01-10 07:51:08

问题


I know this question is asked frequently, but I have not found the solution. I use this code:

open the html tag with

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://ogp.me/ns/fb#">

in the index immediately after the opening tag I entered this

<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    // init the FB JS SDK
    FB.init({
      appId      : 'MY_APP_ID',                        // App ID from the app dashboard
      status     : true,                                 // Check Facebook Login status
      xfbml      : true                                  // Look for social plugins on the page
    });

    // Additional initialization code such as adding Event Listeners goes here
  };

  // Load the SDK asynchronously
  (function(){
     // If we've already installed the SDK, we're done
     if (document.getElementById('facebook-jssdk')) {return;}

     // Get the first script element, which we'll use to find the parent node
     var firstScriptElement = document.getElementsByTagName('script')[0];

     // Create a new script element and set its id
     var facebookJS = document.createElement('script'); 
     facebookJS.id = 'facebook-jssdk';

     // Set the new script's source to the source of the Facebook JS SDK
     facebookJS.src = '//connect.facebook.net/it_IT/all.js';

     // Insert the Facebook JS SDK into the DOM
     firstScriptElement.parentNode.insertBefore(facebookJS, firstScriptElement);
   }());
</script>

finally, the button

<fb:like href="http://mydomain.it/" layout="button_count" action="like" show_faces="false" share="false"></fb:like>
<script>
FB.Event.subscribe('edge.create',
 function(href, widget) {
    alert('Ti piace ' + href);
});
</script>

the like works, but does not open the alert and I got this error in console.

Uncaught ReferenceError: FB is not defined

Any solutions?


回答1:


See this thread: FB is not defined in Facebook Login.

You are trying to use FB right after your button, and it's not defined yet:

<fb:like href="http://mydomain.it/" layout="button_count" action="like" show_faces="false" share="false"></fb:like>
<script>
FB.Event.subscribe(...
^--- FB isn't defined yet!

Move that FB.Event.subscribe code inside your window.fbAsyncInit function, where you have that comment "Additional initialization code"




回答2:


Another reason why you get this error is because you are missing ; after the fbAsyncInit definition bracket.

window.fbAsyncInit = function() {
};


来源:https://stackoverflow.com/questions/20400457/uncaught-referenceerror-fb-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!