javascript 如何在facebook分享者链接中传递自定义参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22653745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:31:22  来源:igfitidea点击:

how to pass custom parameter in facebook sharer link

javascriptphpfacebookcodeigniterfacebook-sharer

提问by kamesh

Code 1:

代码 1:

<a id="button" rel="nofollow" href="http://www.facebook.com/share.php?" onclick="return fbs_click()" target="_blank">
<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;
        }

Here Title "t" is not working but i want to display custom title, summary, url and imageso, I tried something like this

这里的标题“t”不起作用,但我想显示自定义标题、摘要、网址和图像,因此,我尝试了这样的操作

Code 2:

代码 2:

<a id="button"          href="http://www.facebook.com/sharer.php?
                        s=100
                        &p[url]=<?php echo $pressurl;?>
                        &p[images][0]=http://myurl/images/linkedin_image.png
                        &p[title]=mytitle
                        &p[summary]=containsummary                      "> 

In both the cases nothing happened it automatically get some content (title,image,summary) from the above mentioned url and I want to display custom title,image and summary in facebook share page and i dono how to use og meta tag and what s=100 it means?..

在这两种情况下,什么都没有发生,它会自动从上面提到的 url 获取一些内容(标题、图像、摘要),我想在 facebook 共享页面中显示自定义标题、图像和摘要,我不知道如何使用 og 元标记和什么=100 是什么意思?..

回答by Niraj Shah

Instead of using the sharer.php script on Facebook, you can use the Facebook JavaScript SDK to make a custom share dialog.

您可以使用 Facebook JavaScript SDK 来创建自定义共享对话框,而不是在 Facebook 上使用 sharer.php 脚本。

<!-- include Facebook JavaScript SDK -->

<!-- share link -->
<a href="#" class="share-btn">Share to Facebook</a>

<script type="text/javascript">
function fb_share() {
    // facebook share dialog
    FB.ui( {
        method: 'feed',
        name: "Your Page Title",
        link: "https://www.webniraj.com/link-to-page/",
        picture: "https://stackexchange.com/users/flair/557969.png",
        caption: "Some description here"
    }, function( response ) {
        // do nothing
    } );

}

// add click event to link using jQuery
$(document).ready(function(){
  $('.share-btn').on( 'click', fb_share );
});
</script>

This will create a nice popup for sharing to Facebook, and you can easily change the title, description, and picture attributes in the JavaScript. Working demo available here.

这将创建一个用于分享到 Facebook 的漂亮弹出窗口,您可以在 JavaScript 中轻松更改标题、描述和图片属性。可在此处获得工作演示