But you have to go to the Facebook developer page and paste your article's URL and copy the code given by the Facebook developer tools.
It is not comfortable to copy-paste the code whenever you are writing or posting new articles. so javascript has the solution.
Just follow these simple steps.
You have to load Facebook SDK for Javascript once in your source code by adding this code between <body> </body> tags.
<div id="fb-root"></div> <script async defer crossorigin="anonymous" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1 &version={graph-api-version} &appId={your-facebook-app-id} &autoLogAppEvents=1" nonce="FOKrbAYI"> </script>
Then we have to put the code of like button
<!-- Your like button code --> <div class="fb-like" data-href="#" data-width="" data-layout="standard" data-action="like" data-size="small" data-share="true"> </div>
The attribute data-href= says what is the URL of the article we like and we going to change it dynamically using Javascript.
<script>
var x = getElementByClassName('fb-like');
x.data-href = document.location.href;
</script>
that's all .
our script gets the element with the class name 'fb-like' and set its data-href to our article's URL
document.location.href returns you the URL of the current page.
Thank You for reading and don't forget to use the comment section below to express your thoughts.
.
Comments
Post a Comment