views:

1920

answers:

3

How can I determine via javascript if a user has logged in to my site via facebook connect or not? Based on this result, I would like to show/not show a <fb:login-button> button on the site.

If I did something like this using jquery:

<div id="login"></div>

<script>
if (notLoggedIn)
{
   $("#login").html("<fb:login-button></fb>");
}
</script>

Will it work?

+1  A: 

I don't think you can use jQuery in facebook. They have their own a wrapper for js code. Check also this.

Elzo Valugi
I'm using jquery on my own site/domain name which is using facebook connect, not as an actual application on facebook. Doyou think jquery will work for that?
Click Upvote
You can use all the javascript you like if you deploy your Facebook app as an iframe
Pat James
+4  A: 

I did It this way:

HTML:

<div id="logindisplay">
    <span id="login">
      <fb:login-button background="dark" onlogin="facebook_onlogin_ready();">
      </fb:login-button>
    </span>
</div>

js:

function updateUserBox() {
    var user_box = document.getElementById("login");
    user_box.innerHTML = "<span><fb:name uid=loggedinuser useyou='false'></fb:name><fb:profile-pic uid=loggedinuser facebook-logo='true'></fb:profile-pic></span>";
    FB.XFBML.Host.parseDomTree();
        } 

FB_RequireFeatures(["Connect"], function() {
            FB.init("appkey", "/Content/xd_receiver.htm", { "ifUserConnected": updateUserBox });
            FB.Connect.showPermissionDialog("publish_stream,status_update");
            FB.Connect.requireSession();
        });
CD
Have you noticed that when you refresh the page, the original Login button shows up, then it switches to your profile picture? How do you avoid having this happen?
Jack Marchetti
+1  A: 

I don't think jQuery will stop you from inserting non-standard HTML tags, but you'll definitely want to change your closing tag from </fb> to </fb:login-button>.

Beyond that, whenever you insert XFBML tags into your DOM after page load, you'll need to request that the FB API re-parses the DOM. This should do just that:

FB.ensureInit(function(){
  FB.XFBML.Host.parseDomTree();
});
jmikola