SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Aug 2011
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Facebook connect javascript problem

    I added a custom facebook icon to my site. What I want to do is when user press on the facebook icon the facebook connect script should be triggered. I added these two scripts to the <head> of my site:

    <script type="text/javascript">

    window.fbAsyncInit = function() {
    FB.init({appId: '119245164837280', status: true, cookie: true,
    xfbml: true});

    FB.getLoginStatus(function (response) {
    if (response.session) {
    //Do something,User is logged in.
    }
    else {

    }
    });
    FB.Event.subscribe('auth.login', function (response) {
    // When login process completes this event is called.
    Var userid=response.session.uid; // user id of logged in user
    Var accesstoken=response.session.access_token;// access token

    });
    FB.Event.subscribe('auth.logout', function (response) {
    //User is loggin out.
    });


    });

    };
    (function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
    '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
    }());
    </script>


    <script type="text/javascript">

    FB.login(function(response) {
    if (response.authResponse) {
    console.log('Welcome! Fetching your information.... ');
    FB.api('/me', function(response) {
    console.log('Good to see you, ' + response.name + '.');
    FB.logout(function(response) {
    console.log('Logged out.');
    });
    });
    } else {
    console.log('User cancelled login or did not fully authorize.');
    }
    }, {scope: 'email'});

    </script>

    Also i added `<div id="fb-root"></div> right after <body <?php body_class(); ?>>`, but when I load my site I keep getting these error messages: document.getElementById("fb-root") is null and FB is not defined [Break On This Error] FB.login(function(response) { .

    my html:

    <a id="facebookiconid" onclick="FB.login()">Login</a>

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,696
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Donatas View Post
    but when I load my site I keep getting these error messages: document.getElementById("fb-root") is null and FB is not defined [Break On This Error] FB.login(function(response) { .
    It sounds liek you need to load some kind of facebook code library, so that the page knows how to handle those FB objects.

    The facebook for websites page should be of some help, possibly around the authentication section of the page.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •