Home > Back-end >  How to make facebook share button to user facebook application
How to make facebook share button to user facebook application

Time:12-19

How can I make a button on my website when someone from a mobile device clicks on the button to share the link via Facebook app NOT via browser ?

This code works for Facebook messenger application.

   $('.messenger').click(function(){
      window.open('fb-messenger://share?link='   encodeURIComponent(link)   '&app_id='   encodeURIComponent(app_id));
    })

I tried to change the code to the code below but the code below is only opening the Facebook application without insert sharing link

$('.fb-app').click(function(){
  window.open('fb://share?link='   encodeURIComponent(link)   '&app_id='   encodeURIComponent(app_id));
})

CodePudding user response:

From the answer here, may still work:

$('.fb-app').click(function(){
  window.open('fb://faceweb/f?href='   
    encodeURIComponent('https://m.facebook.com/sharer.php?u='   
      encodeURIComponent(link)));
})

CodePudding user response:

To create a button on your website that allows users to share a link via the Facebook app on a mobile device, you can use the share dialog provided by the Facebook JavaScript SDK. Here is an example of how you can do this:

1:First, include the Facebook JavaScript SDK in your HTML file:

<script src="https://connect.facebook.net/en_US/sdk.js"></script>

2:Next, create a button element in your HTML file:

<button id="share-button">Share on Facebook</button>

3:Then, add some JavaScript to your HTML file or a separate JavaScript file to handle the click event on the button and open the share dialog:

const shareButton = document.getElementById('share-button');

shareButton.addEventListener('click', function() {
  // Replace YOUR_LINK_HERE with the actual link you want to share
  const link = 'YOUR_LINK_HERE';

  // Open the share dialog
  FB.ui({
    method: 'share',
    href: link,
  }, function(response){});
});

4:Finally, initialize the Facebook JavaScript SDK by adding the following code to your HTML file or JavaScript file:

FB.init({
  appId      : 'YOUR_APP_ID',
  xfbml      : true,
  version    : 'v8.0'
});

Replace YOUR_APP_ID with your actual Facebook app ID. You can create a Facebook app and obtain an app ID by following the instructions here.

Note that this will only work if the user has the Facebook app installed on their mobile device. If the Facebook app is not installed, the link will be shared via the user's web browser instead.

  • Related