มีวิธีใดบ้างในการสร้างปุ่มแชร์บน Facebook ที่โพสต์ข้อความที่กำหนดเองบนผนังหรือฟีดข่าว?
มีวิธีใดบ้างในการสร้างปุ่มแชร์บน Facebook ที่โพสต์ข้อความที่กำหนดเองบนผนังหรือฟีดข่าว?
คำตอบ:
เราใช้สิ่งนี้ [use in one line]:
<a title="send to Facebook" 
  href="http://www.facebook.com/sharer.php?s=100&p[title]=YOUR_TITLE&p[summary]=YOUR_SUMMARY&p[url]=YOUR_URL&p[images][0]=YOUR_IMAGE_TO_SHARE_OBJECT"
  target="_blank">
  <span>
    <img width="14" height="14" src="'icons/fb.gif" alt="Facebook" /> Facebook 
  </span>
</a>
<a class="facebook" target="_blank" onclick="return !window.open(this.href, 'Facebook', 'width=640,height=300')" href="http://www.facebook.com/sharer/sharer.php?u=YOUR_URL">Facebook</a>ฯลฯ
                    หากต้องการให้พารามิเตอร์ที่กำหนดเองกับ facebook แชร์จะดีกว่าเพื่อให้เฉพาะลิงก์และ facebook จะได้รับTitle + Description + Pictureโดยอัตโนมัติจากเพจที่คุณกำลังแชร์ ในการ "ช่วย" facebook API ให้ค้นหาสิ่งเหล่านั้นคุณสามารถใส่สิ่งต่อไปนี้ในส่วนหัวของเพจที่คุณกำลังแชร์:
<meta property="og:title" content="title" />
<meta property="og:description" content="description" />
<meta property="og:image" content="thumbnail_image" />
หากเพจไม่ได้อยู่ภายใต้การควบคุมของคุณให้ใช้สิ่งที่AllisonCแบ่งปันด้านบน
ใช้ปุ่ม / ลิงค์ / ข้อความของคุณเองจากนั้นคุณสามารถใช้ป๊อปอัปประเภทมุมมองโมดอลด้วยวิธีนี้:
<script type= 'text/javascript'>
$('#twitterbtn-link,#facebookbtn-link').click(function(event) {
var width  = 575,
    height = 400,
    left   = ($(window).width()  - width)  / 2,
    top    = ($(window).height() - height) / 2,
    url    = this.href,
    opts   = 'status=1' +
             ',width='  + width  +
             ',height=' + height +
             ',top='    + top    +
             ',left='   + left;
window.open(url, 'twitter', opts);
return false;
});
</script>
โดยที่ twitterbtn-link และ facebookbtn-link เป็นรหัสของจุดยึด
if( window.open(.....) ) event.preventDefault();แทนที่จะเพียงแค่ `ส่งกลับเท็จ˙! - วิธีนี้จะป้องกันพฤติกรรมเริ่มต้นเท่านั้น (ลิงก์เปิด) เมื่อเปิดหน้าต่าง - ให้ทางเลือกเมื่อมันไม่ใช่ ...
                    ใช้ฟังก์ชันนี้ที่ได้มาจากลิงค์ที่ให้มาโดย IJas
function openFbPopUp() {
    var fburl = '';
    var fbimgurl = 'http://';
    var fbtitle = 'Your title';
    var fbsummary = "your description";
    var sharerURL = "http://www.facebook.com/sharer/sharer.php?s=100&p[url]=" + encodeURI(fburl) + "&p[images][0]=" + encodeURI(fbimgurl) + "&p[title]=" + encodeURI(fbtitle) + "&p[summary]=" + encodeURI(fbsummary);
    window.open(
      sharerURL,
      'facebook-share-dialog', 
      'width=626,height=436'); 
    return  false;
}
หรือคุณสามารถใช้ฟังก์ชันล่าสุดของ FB.ui หากใช้ FB JavaScript SDK สำหรับฟังก์ชันการโทรกลับที่ควบคุมได้มากขึ้น
อ้างอิง: FB.ui
    function openFbPopUp() {
        FB.ui(
          {
            method: 'feed',
            name: 'Facebook Dialogs',
            link: 'https://developers.facebook.com/docs/dialogs/',
            picture: 'http://fbrell.com/f8.jpg',
            caption: 'Reference Documentation',
            description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
          },
          function(response) {
            if (response && response.post_id) {
              alert('Post was published.');
            } else {
              alert('Post was not published.');
            }
          }
        );
    }
คุณมีหลายทางเลือก:
คุณสามารถปรับแต่งกล่องโต้ตอบการแชร์ของ Facebook ได้โดยใช้ JavaScript SDK แบบอะซิงโครนัสที่จัดหาโดย Facebook และตั้งค่าพารามิเตอร์
ดูรหัสต่อไปนี้:
<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: 'URL which you would like to share ',
          picture: ‘URL of the image which is going to appear as thumbnail image in share dialogbox’,
          caption: 'Caption like which appear as title of the dialog box',
          description: 'Small description of the post',
          message: ''
        }
      );
    });
  });
</script>
ก่อนคัดลอกและวางโค้ดด้านล่างนี้คุณต้องเริ่มต้น SDK และตั้งค่าไลบรารี jQuery ก่อน โปรดคลิกที่นี่เพื่อทราบวิธีการตั้งค่าข้อมูลแบบทีละขั้นตอน
นี่เป็นวิธีแก้ปัญหาปัจจุบัน (ธันวาคม 2014) และใช้ได้ดีทีเดียว มันมีคุณสมบัติ
<a onclick="return !window.open(this.href, 'Share on Facebook', 'width=640, height=536')" href="https://www.facebook.com/sharer/sharer.php?u=href=$url&display=popup&ref=plugin" target="_window"><img src='/_img/icons/facebook.png' /></a>
ควรกำหนด $ url var เป็น URL ที่จะแชร์
นี่คือฟีดข้อความโต้ตอบง่ายๆที่ Facebook เสนอ อ่านลิงค์รายละเอียดเพิ่มเติมได้ที่นี่
คุณสามารถรวมแนวคิดของ AllisonC เข้ากับwindow.openฟังก์ชัน:
 http://www.w3schools.com/jsref/met_win_open.asp
function openWin(url) {
    myWindow = window.open(url, '', 'width=800,height=400');
    myWindow.focus();
}
จากนั้นในแต่ละลิงก์คุณจะเรียกใช้ฟังก์ชัน openWin ด้วย URL โซเชียลเน็ตเวิร์กที่ถูกต้อง