0

I'm trying to implement custom button for Facebook Login by using FB JS SDK. Problem is - when I'm calling function fb_login() console said that function is undefined. How should I change this code? JS Part:

<script>
        $(document).ready(function () {
            window.fbAsyncInit = function () {
                FB.init({
                    appId: 'APP_ID',
                    status: true,
                    cookie: true,
                    xfbml: true,
                    version: 'v2.0'
                });
            }
            function fb_login() {
                FB.login(function (response) {
                    if (response.status === 'connected') {
                        var uid = response.authResponse.userID;
                        var url = "http://graph.facebook.com/" + uid + "/picture?type=large";
                        var accessToken = response.authResponse.accessToken;
                        var form = document.createElement("form");
                        form.setAttribute("method", 'post');
                        form.setAttribute("action", 'FacebookLogin.ashx');
                        var field = document.createElement("input");
                        field.setAttribute("type", "hidden");
                        field.setAttribute("name", 'accessToken');
                        field.setAttribute("value", accessToken);
                        var field1 = document.createElement("input");
                        field1.setAttribute("type", "hidden");
                        field1.setAttribute("name", 'img');
                        field1.setAttribute("value", url);
                        form.appendChild(field);
                        form.appendChild(field1);
                        document.body.appendChild(form);
                        form.submit();
                    }
                    else if (response.status === 'not_authorized') {
                        FB.login();
                    } else {
                        FB.login();
                    }
                });
            }
            (function (d) {
                var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
                if (d.getElementById(id)) { return; }
                js = d.createElement('script'); js.id = id; js.async = true;
                js.src = "//connect.facebook.net/ru_RU/all.js";
                ref.parentNode.insertBefore(js, ref);
            }(document));
        });
    </script>

An so here is HTML part:

<div id="fb-root"></div>
    <div class="lvl2 b-visible">
            <a class="btn-fc" onclick="return fb_login();" href="#">Authorize using FB</a>
        </div>

P.S. I read already answers on similar questions (like this one). Solutions offered completely similar to my question, but it's not working.

Community
  • 1
  • 1
andrey.shedko
  • 2,994
  • 9
  • 51
  • 108

1 Answers1

0

Your fb_login() function is defined outside of the scope for which your button can call it. If you want fb_login() inside of the $(document).ready(function(){});, and to still call it from your <a>, you'll want to attach the function to the global variable scope using something like:

$(document).ready(function () {
  // window.fbAsyncInit code here
  window.fb_login = function fb_login() {
     // Your existing fb_login() code here
  }
  // Code here
});

You can see a similar question/answer here: Calling function inside jQuery document ready

And you can read more about JavaScript variable scope here: http://msdn.microsoft.com/en-us/library/ie/bzt2dkta(v=vs.94).aspx

Community
  • 1
  • 1
Zac Clancy
  • 213
  • 2
  • 9