在个人网站上插入Facebook分享按钮

时间:2015-05-21 21:23:28

标签: facebook facebook-javascript-sdk facebook-social-plugins facebook-share

我想在我的网站上放一个按钮,打开一个弹出窗口供用户在其个人资料上分享网址。

点击“获取代码”后,我按照here所示的步骤操作:

1. Include the Javascript SDK on your page once, ideally right after the opening <body> tag.

2. Place the code for your plugin wherever you want the plugin to appear on your page.

但是我没有按钮就得到一个空白的div。缺少什么?我需要在Facebook开发者注册吗?我是否需要在<head>

中加入一些javascript src

在Twitter上非常简单,您只需要从here

获取代码

编辑:我想要像youtube facebook分享的内容。

1 个答案:

答案 0 :(得分:2)

正如评论中所指出的那样,Facebook建议的默认方法不起作用,或者说share button documentation建议的方式不够明确。建议的This answer似乎有效。请注意,您需要一个Facebook App ID

或者,这个answer允许一个简单的链接,然后您必须使用CSS设置自己的样式,此替代的好处是可以使设计与您的设计保持一致的共享按钮网站,如果你只想要香草蓝的Facebook按钮,那么继续这个答案。

Squarespace还详细介绍了添加类似facebook的按钮的默认方式,我试图让它在jsFiddle中工作的尝试并不成功。

  1. 获取AppID并将Facebook脚本嵌入您的网站。

    • 此步骤在official documentation中详细说明,您只需生成自己的AppID,然后复制粘贴脚本代码(通常在<body>标记之后。
  2. 按照this answer中的说明为下一段代码添加一个jQuery库。

  3. 共享对话框脚本:

  4. -

    <script type="text/javascript">
    $(document).ready(function(){
    $('#share_button').click(function(e){
    e.preventDefault();
    FB.ui(
    {
    method: 'feed',
    name: 'This is the content of the "name" field.',
    link: 'Your-blog-link',
    picture: ‘http://yourpicture-here’,
    caption: 'yourCaption',
    description: short-description-here
    message: ''
    });
    });
    });
    </script>
    

    还可以添加更多参数来自定义按钮。

    最后,添加您想要成为共享按钮的图像/元素:

    类似于:<img src = "share_button.png" id = "share_button">