简体   繁体   English

onClick:在Facebook上分享图片

[英]onClick: share image on Facebook

I would like to setup an onClick function to an image on my new website that launch Facebook sharer. 我想在我的新网站上启动Facebook共享器的图像上设置一个onClick功能。

Here are some pieces of code that may help understand what I'm talking about 以下是一些可能有助于理解我所说的内容的代码

<div id="mImageBox">
<img id='my_image' src='' width="auto" height="auto"/>
</div>

The image src is blank because it's injected by another JavaScript function by using "my_image" 图像src是空白的,因为它是由另一个JavaScript函数通过使用“my_image”注入的

so, I tried to setup the onClick function with this method: 所以,我尝试使用此方法设置onClick函数:

<script>function fbs_click() {u=location.href;t=document.title;window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;}</script>

<div id="mImageBox">
<a rel="nofollow" href="http://www.facebook.com/share.php?u=<;url>" onclick="return fbs_click()" target="_blank"><img id='my_image' src='' width="auto" height="auto"/></a>
</div>

The onClick function works well, however, it links to the page where the img is located and not the img itself! onClick函数运行良好,但它链接到img所在的页面而不是img本身!

Have you got any suggestion for me? 你有什么建议吗?

try this: 试试这个:

<div id="mImageBox">
<img id='my_image' src='' alt='' width="auto" height="auto" onclick="fbs_click(this)"/>
</div>

<script>
     function fbs_click(TheImg) {
     u=TheImg.src;
     // t=document.title;
    t=TheImg.getAttribute('alt');
    window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');return false;
}
</script>

You don't need to use <a> tag (surrounding the <img> tag) for case that JS disabled, because in that case the JS that should inject the src attribute will not work also. 对于JS禁用的情况,您不需要使用<a>标签(围绕<img>标签),因为在这种情况下,应该注入src属性的JS也不会起作用。

Just call below function and you can share Image with your Link and Text on Facebook. 只需调用以下功能,您就可以在Facebook上与您的链接和文本共享图像。

function sharefbimage() {
    FB.init({ appId: `your appid`, status: true, cookie: true });
    FB.ui(
    {
        method: `share`,
        name: 'Facebook Dialogs',
        href: $(location).attr('href'),
        link: 'https://developers.facebook.com/docs/dialogs/',
        picture: 'your image url',
        caption: 'Ishelf Book',
        description: 'your description'
    },
    function (response) {
        if (response && response.post_id) {
            alert('success');
        } else {
            alert('error');
        }
    }
);

Since Facebooks Javascript API 2.9 is dead since last week the solution above doesn't work any more ( I took me half a day to find this out!!! ). 自从上周以来Facebook上的Javascript API 2.9已经死了,上面的解决方案不再适用了(我花了半天时间才找到它!!!)。 In v2.10 the picture-parameter of FB.ui doesn't exist any more. 在v2.10中,FB.ui的图片参数不再存在。 But I found a workaround generating an individual URL parameter for each image so Facebook has to scrape Open Graph data for each shared image individually. 但我找到了一种解决方法,为每个图像生成一个单独的URL参数,因此Facebook必须单独为每个共享图像刮取Open Graph数据。 I've shared my code in this post for your reference. 我在这篇文章中分享了我的代码供您参考。

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

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