简体   繁体   English

自定义Facebook共享按钮不起作用

[英]Custom Facebook share button does not work

Can somebody explain why my custom Facebook share button does not work? 有人可以解释为什么我的自定义Facebook分享按钮不起作用吗? Whenever I click the button it the share window just does not show up. 每当我单击按钮时,共享窗口就不会显示。 I've tried it as html file on an actual webserver, and I've read everything in the facebook docs regarding the share button. 我已经在实际的Web服务器上尝试将其作为html文件使用,并且已经阅读了Facebook文档中有关“共享”按钮的所有内容。

When using the same code provided by facebook it does work. 当使用facebook提供相同代码时,它确实起作用。

I do get this error though when clicking on the button. 我确实会在单击按钮时收到此错误

This is my code. 这是我的代码。 (Without the CSS because its a bit more, If you need it I can add it.) (没有CSS,因为它还有更多,如果需要,我可以添加它。)

<script type="text/javascript" src="https://connect.facebook.net/en_US/sdk.js#xfbml=1"></script>

<div class="page-header">
  <h1>Share Dialog</h1>
</div>
<p>Click the button below to trigger a Share Dialog</p>
<button class="fill" id="fb-share-button">Share on Facebook</button>

<script>
document.getElementById('fb-share-button').onclick = function() {
  FB.ui({
    method: 'share',
    display: 'popup',
    href: 'https://nureinberg.de',
  }, function(response){});
}
</script>

You need to initialize the FB SDK, 您需要初始化FB SDK,

This includes the FB.init function and having the <div id="fb-root"></div> element. 这包括FB.init函数,并具有<div id="fb-root"></div>元素。

Check out this pen - it works just fine minus the App ID of course, but the popup portion works: 签出这支笔-当然可以,减去App​​ ID即可,但是弹出部分有效:

https://codepen.io/xhynk/pen/MVKerM https://codepen.io/xhynk/pen/MVKerM

I also added the version ID to the sdk.js script ( https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.7 ) 我还将版本ID添加到了sdk.js脚本( https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.7

Try adding a version and the App ID to the JS SDK source: 尝试将版本和App ID添加到JS SDK源:

https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.12&appId=<your-app-id>

Although, i would recommend using the asynchronous way to load the JS SDK: https://developers.facebook.com/docs/javascript/quickstart 虽然,我建议使用异步方式来加载JS SDK: https : //developers.facebook.com/docs/javascript/quickstart

If you do not want to create an App, use sharer.php with a standard anchor-tag: 如果您不想创建一个应用程序,请使用带有标准锚标记的sharer.php:

https://www.facebook.com/sharer/sharer.php?u=<encoded-url>

Thank you for all your answers. 感谢您的所有答复。 It works now with the following code. 现在,它可以与以下代码一起使用。

I've also created a codepen for it. 我还为此创建了一个代码笔

window.fbAsyncInit = function() {
  FB.init({
    appId      : 'your-app-id',
    cookie     : true,
    xfbml      : true,
    version    : 'v2.7'
  });

  FB.AppEvents.logPageView();

};

document.getElementById('fb-share-button').onclick = function() {
  FB.ui({
    method: 'share',
    display: 'popup',
    href: window.location.href,
  }, function(response){});
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM