我正在尝试为我的网站创建自定义 Facebook 共享功能,并且我正在使用 Facebook 共享器 URL 在弹出窗口中打开 Facebook 共享。但是,由于某种原因它不起作用。
我正在做的事情很简单:
function shareFB(){
var img=$('#fb_img').html();
var text=$('#fb_share').html();
var url=$('#fb_url').html();
var full_url='http://www.facebook.com/sharer.php?s=100&p[title]=MyTitle&p[summary]='+text+'&p[url]='+url+'&&p[images][0]='+img;
window.open(full_url, 'sharer', 'toolbar=0,status=0,width=548,height=325');
}
奇怪的是,我给 img
、text
和 url
赋予什么值并不重要,即使我使用了所有值其中。在下面的示例中,我完全删除了 img
并设置了 text="whatever"
和 url="http://www.domain.com/stats_leagues.php “
。
https://www.facebook.com/sharer/sharer.php?s=100&%3Bp[title]=SportFant&%3Bp[summary]=whatever&%3Bp[url]=http%3A%2F%2Fwww.domain.com%2Fstats_leagues.php
但是它不起作用 - 它只是打开一个空的弹出窗口。为什么?我已经在 Facebook 分享中使用了相同的 URL 格式十几次,而且它总是有效。即使将上面的格式化 URL 复制/粘贴到我的浏览器中也只会打开一个空白页面。
最佳答案
我不知道你从哪里得到所有这些参数,比如 s=
, p[title]
等,可能来自某些旧版本的 facebook 分享?
在 Facebook 上 share button文档,在常见问题解答下,它说:
Yes, although we recommend using the Share button to offer the simplest and most consistent experience for people using your site, you can invoke the Share Dialog using a link:
<a href="https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fparse.com" target="_blank">
所以只需将代码更改为:
function shareFB() {
var url = $('#fb_url').html(); #encode url if it is not already
var full_url = 'http://www.facebook.com/sharer.php?u='+url;
window.open(full_url, 'sharer', 'toolbar=0,status=0,width=548,height=325');
}
常见问题解答还指出,Facebook 会抓取您的页面以获取其他信息,例如标题、图像等。它会从您的页面中抓取您的 OG 元标记。调试它here .
关于javascript - 为什么这个 Facebook 共享代码不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20680273/