创建自定义Facebook共享链接

时间:2014-03-24 21:50:00

标签: html facebook html5 fb.ui

所以之前已经问过这个问题,但是没有人想以我想要的方式去做,所以在我尝试解释的时候请耐心等待。 FB.ui API有一些我认为有用的功能,主要是我可以动态更改描述。所以,我想把FB.ui放到一个用户可以点击的链接中,然后会出现一个弹出窗口,他们可以在那里共享我的网页。到目前为止我所拥有的是:

<div id="fb-root"></div>
    <script>
 function shareFB(){

      window.fbAsyncInit = function() {
        FB.init({
          appId      : '42352352356463',
          status     : true,
          xfbml      : true
        });
        FB.ui(
  {
   method: 'feed',
   name: 'name',
   caption: 'caption',
   description: (

    a
   ),
   link: 'http://www.image.com/',
   picture: 'http://www.image.com/static/3.png'
  },
  function(response) {
    if (response && response.post_id) {
      alert('Post was published.');
    } else {
      alert('Post was not published.');
    }
  }
);
      };

      (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/en_US/all.js";
         fjs.parentNode.insertBefore(js, fjs);
       }(document, 'script', 'facebook-jssdk'));

}
</script>

然后我在这里有一个链接:

<a class="links" onclick='shareFB()' href="#"> Share! </a>  

但这不起作用。为什么不!

1 个答案:

答案 0 :(得分:1)

您在点击按钮时加载了facebook SDK,这是加载它的错误方法,将脚本更改为:

<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
    FB.init({
      appId      : '42352352356463', // App ID
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });
};


// Load the SDK Asynchronously
(function(d){
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement('script'); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/pt_PT/all.js";
    ref.parentNode.insertBefore(js, ref);
}(document));


function shareFB(){
    var obj = {
        method: 'feed',
        name: 'name',
        caption: 'caption',
        description: 'description',
        link: 'http://www.image.com/',
        picture: 'http://www.image.com/static/3.png'
    };

    function share(response){
        if (response && response.post_id) {
          alert('Post was published.');
        } else {
          alert('Post was not published.');
        }
    }
    FB.ui(obj, share);
};
</script>