Reputation: 350
hi below is my code to change facebook share button dynamically
<script>
function f1() {
$(function () {
var id = location.href.replace(/.*pid=/, '');
$.galleriffic.gotoImage(id);
{ window.open("https://www.facebook.com/sharer/sharer.php?u=" + escape(id) + "&t=" + document.title, '', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=300,width=600'); return false; }
})
};
</script>
<div onclick="f1()" id="sharenew" class="fb-share-button" data-href=""></div>
in the given code facebook share button accept the url but i want to pass url with image id which get with # value so here my url become "http://writopedia.org/THCComment.aspx#2" but it does not accept by facebook so help me.
Upvotes: 3
Views: 6474
Reputation: 73984
Use FB.ui
share for dynamic URLs: https://developers.facebook.com/docs/sharing/reference/share-dialog
Basic example from the docs:
FB.ui({
method: 'share',
href: 'https://developers.facebook.com/docs/',
}, function(response){});
Of course you need to include the JavaScript SDK for that: https://developers.facebook.com/docs/javascript/quickstart/v2.1
...and forget about the share button, just use your own button and call FB.ui on click.
Upvotes: 1
Reputation: 350
the given code is work for me
<script>
function FBShareOp(){
var product_name = jQuery("#title").val();
var description = jQuery("#description").html();
var share_image = jQuery("#share_image").attr('src');
var share_url = jQuery("#share_url").attr('href');
var share_capt = jQuery("#share_capt").val();
var id = location.href.replace(/.*pid=/, '');
$.galleriffic.gotoImage(id);
FB.ui({
method: 'feed',
name: product_name,
link: id,
picture: images,
caption: share_capt,
description: description
}, function(response) {
if(response && response.post_id){}
else{}
});
}
</script>
<a onclick="FBShareOp();" style="color: #fff; background-color: #435EAB; padding: 2px 7px 1px 7px; font-family: 'Helvetica Neue', Helvetica, Arial, 'lucida grande',tahoma,verdana,arial,sans-serif; font-size: 11px; text-decoration: none; border-radius: 2px; float: right; margin-top: 1px; margin-right: 45px;" href="#" title="Share" class="ymsb-fbshare-btn" data-target="popup" id="sharebutton">Share</a>
Upvotes: 5