0

I am trying to add facebook like button inside a div but its not working.

I have included javascript sdk given by facebook. Here is facebook instruction on adding like button https://i.stack.imgur.com/wol2G.jpg and below is my code:

body {
    margin:0px;
    padding:0px;
}
#wrapper {
    width: 100%;
    height: 50px;
    background-color: black;
}
<!DOCTYPE HTML>
<html lang="en">

<head>
    <link rel="stylesheet" href="style.css" />
</head>
<body>
    <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/en_US/sdk.js#xfbml=1&version=v2.3";
            fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));
    </script>
    <div id="wrapper">
        <div class="fb-like" data-href="https://developers.facebook.com/docs/plugins/" data-layout="standard" data-action="like" data-show-faces="true" data-share="true"></div>
    </div>
</body>
</html>
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195

2 Answers2

0

Because of the following line of code, I think you need to test this on a web server:

js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.3";

*Update: It will NOT work locally, even if you add http: in front of the //. I just uploaded a test page using your code to my hosting account and the buttons appeared just fine.

*Update2: This link explains why Facebook requires the site to be hosted rather than local for this button to work: How do I test the Facebook "Like" button on localhost?

Community
  • 1
  • 1
Thinkstr8
  • 126
  • 4
0

i can see you did not do anything wrong. try adding the following before script:

window.fbAsyncInit = function() {
FB.init({
  appId      : 'your fb app id',
  xfbml      : true,
  version    : 'v2.3'
});
};

then this where you want the code to be

  <div
        class="fb-like"
        data-share="false"
        data-width="20"
        data-show-faces="false">
    </div>
Tatenda
  • 679
  • 2
  • 10
  • 24