在facebook上分享链接,如何重新初始化fb obj

时间:2015-01-21 10:00:46

标签: javascript php facebook facebook-javascript-sdk

我正在开发一个项目,该项目需要在弹出窗口中显示facebook,twitter,google +图标以共享链接。

Twitter和Google+对我来说很好。 但是在facebook的情况下,分享图标显示我第一次加载弹出窗口时,但是在我再次加载弹出窗口之后它才会显示,直到刷新主页面。

我认为这是因为,fb obj在第一次加载弹出窗口时已经初始化,阻止了弹出重新加载时重新初始化。

任何人都可以建议,我如何解决这个问题。

我使用了以下代码:

谷歌+

<a href="https://plus.google.com/share?url=<?php echo 'dynamically_created_link_with_querystring';?>" onclick="javascript:window.open(this.href,'', 'menubar=no,toolbar=no,resizable=yes,scrollbars=yes,height=600,width=600');return false;">
<img src="https://www.gstatic.com/images/icons/gplus-16.png" alt="Share on Google+"/></a>

微博

<script>
window.twttr = (function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0],
    t = window.twttr || {};
  if (d.getElementById(id)) return;
  js = d.createElement(s);
  js.id = id;
  js.src = "https://platform.twitter.com/widgets.js";
  fjs.parentNode.insertBefore(js, fjs);

  t._e = [];
  t.ready = function(f) {
    t._e.push(f);
  };

  return t;
}(document, "script", "twitter-wjs"));
</script>
<a href="http://www.twitter.com/intent/tweet?url=<?php echo 'dynamically_created_link_with_querystring';?>" >
<img src="images/Twitter_logo_blue_16.png"/></a>

<script>
(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#xfbml=1&version=v2.0";
          fjs.parentNode.insertBefore(js, fjs);
        }(document, 'script', 'facebook-jssdk'));
</script>
<div id="fb-root"></div>
<div class="fb-share-button" data-href="<?php echo "dynamically_generated_link_with_querstring";?>" data-layout="icon"></div>

1 个答案:

答案 0 :(得分:1)

使用以下代码修复了上述问题:

新Facebook代码

<a href="javascript:fbShare('<?php echo dynamically_generated_link_with_querystring ?>', 520, 350)">
<img src="images/fb_icon.png" height="32"width="32"/>
</a>
<script>
function fbShare(url, winWidth, winHeight) {
    var winTop = (screen.height / 2) - (winHeight / 2);
    var winLeft = (screen.width / 2) - (winWidth / 2);
    url = encodeURIComponent(url);
    window.open('http://www.facebook.com/sharer.php?s=100&p[url]=' + url, 'sharer', 'top=' + winTop + ',left=' + winLeft + ',toolbar=0,status=0,width=' + winWidth + ',height=' + winHeight);
}
</script>

旧的Facebook代码

<script>
(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#xfbml=1&version=v2.0";
      fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
</script>
<div id="fb-root"></div>
<div class="fb-share-button" data-href="<?php echo "dynamically_generated_link_with_querstring";?>" data-layout="icon"></div>