Facebook分享按钮和Ajax



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

我做什么:调用了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>   

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

有什么建议吗

我找到了一个解决方案,使用另一个ajax称为页面,包括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>

最新更新