Search code examples
javascriptfacebookfacebook-sharer

Facebook Share only works if user is logged in?


I'm hoping this isn't a duplicate, but I haven't seen this explained before. I've got a very basic Facebook Share implementation that I'm trying to use:

<a name="fb_share" type="button" share_url="http://www.google.com"></a> <script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"> </script>

The problem is: the FIRST time I click my "share" button, it prompts me to login to facebook. When I login in, it takes me straight to my news feed and does not show the link I'm trying to share. But if I login, close the window and then re-click my "share" button, it shows the link I'm trying to share.

Is there something I'm missing from my code? I'm using IE9 if that makes a difference.

EDIT: It appears to work fine in Chrome, but does NOT work in Safari either.

EDIT 2: After trying to use Juicy Scripter's solution with the Feed Dialog, I am now having trouble with the following code. After I'm prompted to Login to Facebook, I get a red error message that just says "An error has occurred, please try again later." Can anyone see what I might be doing wrong with the following code? Also, is there a "best practice" in getting my link to be the actual Facebook button (like it was for the Share option?)

<a onclick='postToFacebook(); return false;'>FB post</a>

    <script type="text/javascript">
        function postToFacebook() {
            FB.init();
            FB.ui({ method: 'feed', link: "http://www.google.com" });
        }
    </script>`

Many thanks in advance!


Solution

  • As already noted by other answer Share Button is deprecated so it's better to avoid it.

    Use Feed Dialog to publish content on user's feed. For general purposes next code may be used to share current page link (consult documentation for additional parameters):

    FB.ui({method: 'feed', link: document.location.href});