动态更改facebook分享按钮网址

时间:2014-09-29 06:34:13

标签: jquery facebook

下面是我动态更改facebook分享按钮的代码

<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>

在给定的代码中,facebook分享按钮接受网址,但我想传递带有#值的图片ID的网址,所以这里我的网址变为&#34; http://writopedia.org/THCComment.aspx#2&#34;但它不接受facebook所以帮助我。

2 个答案:

答案 0 :(得分:5)

给定的代码对我有用

<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> &nbsp;

答案 1 :(得分:1)

使用FB.ui分享动态网址:https://developers.facebook.com/docs/sharing/reference/share-dialog

来自文档的基本示例:

FB.ui({
  method: 'share',
  href: 'https://developers.facebook.com/docs/',
}, function(response){});

当然,您需要包含JavaScript SDK:https://developers.facebook.com/docs/javascript/quickstart/v2.1

...忘记分享按钮,只需使用自己的按钮,点击即可调用FB.ui。