0

I was wondering how to do this on page load. It is working with "onClick", i.e <a href="#" onClick="FB.logout()">Logout</a> <- Works

but if I have something like this done:

 // INIT (file 1)
 $ ->
    $('body').prepend('<div id="fb-root"></div>')

    $.ajax
        url: "#{window.location.protocol}//connect.facebook.net/en_US/all.js"
        dataType: 'script'
        cache: true

    window.fbAsyncInit = ->
        FB.init(appId: 'xxxxxxxxxxxx', cookie: true)

       $('#facebook_sign_in').click (e) ->
          e.preventDefault()
          FB.login (response) ->
            window.location = '/auth/facebook/callback' if response.authResponse


    // FILE 2

    <script src="file1.js" type="text/javascript"></script> 
    $(document).ready(function(){
        FB.logout(); <- Nothing happens
    });

Debugger says, that FB variable is not defined. But why on earth it would be undefined, if onClick works like a charm ?

So, is it somehow possible ?

Dmitri
  • 2,451
  • 6
  • 35
  • 55

2 Answers2

0

It seems that when you use the FB api it has not been loaded yet.

Using $.ajax() to load the FB all.js script might be the cause, do either add a callback function to it, or load the script in the head of your html.

  • My script in fact, IS located in head. I've even tried to make FB global (window.FB = FB or window.FB.init), but now everything is "undefined" – Dmitri Dec 11 '12 at 09:24
  • I dont mean your script, I mean the script to load the FB stuff: http://connect.facebook.net/en_US/all.js –  Dec 17 '12 at 15:34
0

Please try this.

 $(document).ready(function(){
            FB.logout(function(response) {
               FB.Auth.setAuthResponse(null, 'unknown');
             });
        });    

this refers to Facebook JS SDK FB.logout() doesn't terminate user session