Facebook分享按钮和Ajax

时间:2016-10-10 08:10:00

标签: jquery ajax facebook refresh share

我正在使用不同文章刷新div内容的网站。我必须为每篇文章放置一个Facebook共享按钮。 使用jQuery在Ajax中完成刷新

我做什么: 在调用了article.php页面后,我发送了一个ID值。 URL如下所示:

  

http://www.website.org/index.php?page=article.php&id=892

在index.php页面中,我已经包含:

<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script>
<script>
    // Facebook
    (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/fr_FR/sdk.js#xfbml=1&version=v2.7";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));

</script>

在article.php页面中,

<div id="fb_container" class="fb-share-button" data-href="<?PHP 
   $url = "http://www.website.org/index.php?page=article?id=".$id;
   echo $url; 
   ?>" data-type="button_count">
</div>
<script>
$(document).ready(function() {
    $('meta[property=og:title]').remove(); $('head').append('<?PHP echo $titre['contenu']; ?>');
    // Same for the others meta tags

    FB.XFBML.parse(document.getElementById('fb_container'));
});
</script>   

元标记已更新,没关系。 但共享按钮不起作用: - 在第一次加载时,它是可见的,但共享页面不显示图像和元信息 - 当我们阅读另一篇文章时,不会显示分享按钮

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

我找到了一个使用另一个名为page的页面的解决方案,包括Facebook链接和FB.ui函数:

在index.php中:

  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'Your id app',
      xfbml      : true,
      version    : 'v2.8'
    });
  };

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

function openFbPopUp(fburl,fbimgurl,fbtitle,fbsummary) {
FB.ui(
          {
            method: 'feed',
            name: fbtitle,
            link: fburl,
            picture: fbimgurl,
            caption: fbtitle,
            description: fbsummary
          }
        );  
}       

在刷新的facebook.php页面中,包括facebook分享链接:

// here the connection to the MySQL database
// read the values of the id element

<a onclick="openFbPopUp('<?PHP echo ($url_fb); ?>','<?PHP echo ("http://".$_SERVER['SERVER_NAME']."/".$photo['url_photo']); ?>','<?PHP echo ($titre['content']); ?>','<?PHP echo (substr(addslashes($description['content']),0,200)."..."); ?>')" value="SHARE">
<img src="images/fb-share.png">
</a>