0

I am implementing facebook share. However I get error Uncaught ReferenceError: FB is not defined. However, if I put code inside getScript function (in code below) then it works, but only at page load. Viewed lots of answers like this, but was not able to get the solution.

    <div id="fb-root"></div>
    <script>
    jQuery(document).ready(function() {
      jQuery.ajaxSetup({ cache: true });
      jQuery.getScript('//connect.facebook.net/en_UK/all.js', function(){
            window.FB.init({
              appId: 'XXX',
            });     
            jQuery('#loginbutton,#feedbutton').removeAttr('disabled');
        });
        FB.ui(
          {
            method: 'feed',
            name: 'Facebook Dialogs',
            link: 'http://XXX.website',
            picture: 'http://XXX.website/logoroundTR.gif',
            caption: 'MyCaption',
            description: 'MyDescription'
          },
          function(response) {
            if (response) {
              alert('Post was published.');
            } else {
              alert('Post was not published.');
            }
          }
        );
    });
    </script>
<button><fb:like href="http://XXX.website/" layout="button_count" action="like" show_faces="true" share="true"></fb:like></button>

p.s.: Second question: How do I link the button to FB.ui function. Any easy way?

Community
  • 1
  • 1
user1517108
  • 2,395
  • 6
  • 32
  • 43

1 Answers1

0

Ok found a workable solution. Slight Hack.

Removed FB.ui and put it in the button onclick and it works great.

So for custom title, caption, image and description for facebook javascript sdk, one can do following:

<div id="fb-root"></div>
<script>
jQuery(document).ready(function() {
  jQuery.ajaxSetup({ cache: true });
  jQuery.getScript('//connect.facebook.net/en_UK/all.js', function(){
        window.FB.init({
          appId: 'appID',
        });     
        jQuery('#loginbutton,#feedbutton').removeAttr('disabled');
    });
});
</script>
<button onclick="javascript:FB.ui({method: 'feed',name: 'Facebook Dialogs',link: 'http://xxxY.com',picture: 'http://xxxy.com/wp-content/img/logoroundTR.gif',caption: 'SandeepCaption',description: 'SandeepDescription'});"> Click Here </button>
user1517108
  • 2,395
  • 6
  • 32
  • 43