How to add Dynamic facebook share button for website.
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>
<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>
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>
- Add this script in your head tag of page that you want share in FB.
- var currentUrl = window.location.href; use to retrieve the current url.
- Above facebook div tag add extra attribute id=fb_share, through this id we set dynamic url using javasript.
- But before that you must create API in facebook https://developers.facebook.com/, that fb will provide a key.
- To insert share button follow the link https://developers.facebook.com/docs/plugins/share-button/
- 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>
Thank you
ReplyDelete