0

I want to have a "facebook share" link or button on a web page. The idea is to to share a link and a brief description, identical to twiter. No image or title of page is needed

In the past, the following link worked:- "http://www.facebook.com/sharer.php?u=" + complete_url + message

Now, facebook ignores the message and it discards the complete url link. Instead it only displays the title of the homepage.

I found this answer on stack overflow:- Make my own custom Facebook share button.

Having tried it (the script below) - I get the error: 'FB' is undefined

I'd be grateful if you could tell me:- 1. what am I doing wrong? 2. do we have to download the sdk.js from facebook server everytime?

The script is like this:

<html><head>
<script>
function onClick() {
        FB.ui({
            method: 'share',
            href: 'the_complete_url_to_specific_page'
        });
    }

    window.fbAsyncInit = function() {
        FB.init({
            appId      : 'my_facebook_id',
            xfbml      : true,
            version    : 'v2.3'
        });
    };

    (function(d, s, id){
        var js, fjs = d.getElementsByTagName(s)[0];
        if (d.getElementById(id)) {return;}
        js = d.createElement(s); js.id = id;
        js.src = "//connect.facebook.net/en_US/sdk.js";
        fjs.parentNode.insertBefore(js, fjs);
    }(document, 'script', 'facebook-jssdk'));
</script>
</head>
<body>

<a href='#' onclick='onClick() ;return false;'> share on FacebookR</a>

</body>
</html>
Community
  • 1
  • 1
Jess
  • 9
  • 5
  • I meant to add: versions of the sdk change (now it is v2.5), do we need to change the version in the script everytim? – Jess Feb 29 '16 at 20:50

1 Answers1

1

First of all: No, you do not need to change the API version. Only if you need to use something that is only available in newer versions, check out the changelog once in a while: https://developers.facebook.com/docs/apps/changelog

You get the "FB is undefined" error if the JavaScript is not loaded (yet). Most likely you are not trying your code on a real server (it can also be localhost) but from your file system. You can try adding "https" to the source like this, or (better) test the code on a real server:

js.src = "https://connect.facebook.net/en_US/sdk.js";

The App ID is NOT your User ID, it´s an ID you get after creating an App here: https://developers.facebook.com/apps

Btw, the user can enter the message right when the Share Dialog pops up.

andyrandy
  • 72,880
  • 8
  • 113
  • 130
  • Thank you. Have changed the line to js.src = "https://connect.facebook.net/en_US/sdk.js" and tried it on a localhost server. Now a pop-up window appears (an improvement) but there is the message "invalid app ID" !! Isn't the app ID the same as the ID number in the profile or is it another one - which I don't have? Thanks again. – Jess Feb 29 '16 at 21:42
  • what? why would you remove the slashes? – andyrandy Feb 29 '16 at 22:00
  • the app id is NOT your facebook id..it´s the id you get after creating an app. you really need to start reading the facebook docs from the beginning ;) - i will add a link to my answer. – andyrandy Feb 29 '16 at 22:00
  • Thank you for the answer. I am going to try to find out I just thought it would be a straightforward step, like twitter + telegram and G+. I will report here once done. Still, I really would like to include a message in the same script. in this particular case, It would not be that useful at all if it were left for users to add their own messages. Thanks anyway! – Jess Mar 01 '16 at 00:38
  • I honestly cannot find how to create an app in facebook. Have read this tutorial: How to Create a Facebook Application to Get an App ID for your Website at http://www.hyperarts.com/blog/how-to-create-a-facebook-application-for-website-integration/ but in the Facebook Developers page I just couldn't see the "Set up New App" that the tutorial mentions. Hints / Guidance would be greatly appreciated. Thanks – Jess Mar 01 '16 at 01:31
  • I wonder why Facebook have made it so complicated a task, while in twitter, G+ and Telegram, it can be done with a single line :( – Jess Mar 01 '16 at 01:38
  • prefilling the message is not allowed, you need to read the platform policy: https://developers.facebook.com/policy/ – andyrandy Mar 01 '16 at 08:17
  • about creating a new app: just open the link in my answer, there is a button for creating a new app. just try it. it´s actually not more complicated than twitter or g+, but you have to keep in mind that the facebook api is a LOT bigger. – andyrandy Mar 01 '16 at 08:17
  • Thank you, luschn, I got it. – Jess Mar 01 '16 at 11:06