FB分享按钮在页面加载时消失

时间:2017-01-10 06:07:49

标签: javascript ruby-on-rails facebook facebook-graph-api

我有以下haml

.share-fb-pop-up
.fb-contain
  .fb-share-button{"data-href" => "https://fls.net/proficiency_test", "data-layout" => "button", "data-mobile-iframe" => "false", "data-size" => "small"}
    %a.fb-xfbml-parse-ignore{:href => "https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Ffls.net%2Fproficiency_test&src=sdkpreparse", :target => "_blank"} Share
.pop-button-div
  %button
    Close

我加载了这样的javascript:

:javascript
 window.fbAsyncInit = function() {
  FB.init({
    appId      : '<my-app-id>',
    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'));

我知道它有效,因为我看到它生成文本“分享”,当我从.fb-xfbml-parse-ignore标签中删除类a时,它正确链接并通过脸书分享。是否与运行localhost或rails在页面中调用SDK有关?

谢谢!

1 个答案:

答案 0 :(得分:1)

在加载共享按钮之前调用facebook-jssdk初始化脚本。

  1. 为您的按钮id
  2. 使用JS找出按钮加载的时间并调用此函数:FB.XFBML.parse(document.getElementById("<given-id>"))