Skip to main content

JavaScript Dynamic URL facebook share button

How to  add Dynamic facebook share button for website.
                                   Here we have code to add dynamic facebook share button that we can use in  Aps.Net, Php or any other html document website page. follow the steps.
                                                     <script type="text/javascript" language="javascript">
            window.onload = function () {
                var currentUrl = window.location.href;                
                document.getElementById("fb_share").setAttribute("data-href", currentUrl);               
            }
</script>
<div class="fb-share-button" id="fb_share"   data-layout="standard" data-action="like" data-size="small" data-show-faces="true" data-share="true"></div> 
                                     
  1. Add this script in your head tag of page that you want share in FB.
  2.  var currentUrl = window.location.href;  use to retrieve the current url.
  3. Above facebook div tag add extra attribute id=fb_share, through this id we set dynamic url using javasript. 
  4. But before that you must  create API in facebook https://developers.facebook.com/, that fb will provide a key.
  5.  To insert share button follow the link https://developers.facebook.com/docs/plugins/share-button/
  6. The code will given like this

<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/ta_IN/sdk.js#xfbml=1&version=v2.10&appId=APIKey";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

Comments

Post a Comment