views:

216

answers:

1

I don't know why it's not finding the libraries I loaded for JS:

<html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <head id="Head1" runat="server">
        <link href="Content/Styles/Site.css" rel="stylesheet" type="text/css" />
        <title></title>
        <script src="Content/js/jquery-1.3.2.min.js" type="text/javascript"></script>
        <script src="Content/js/jquery.cookie.js" type="text/javascript"></script>
        <script src="Content/js/FacebookAPI.js" type="text/javascript"></script>
    </head>
    <body>


        <form id="form1" runat="server">
            <div id="facebookPhotos-iFrameContent">
                <div>
                    <p>Log into facebook by clicking on the button below</p>
                    <p id="facebook-LoginButtonContainer">

                    <input type="image" id="btnFacebookLogin" src="images/facebookLoginBtn.jpg" />

                    </p>
                    <p><asp:DropDownList ID="facebookAlbumDropdown" runat="server" /></p>
                    <div id="facebookPhotosContainer" />
                </div>
            </div>

            <div id="fb-root"></div>

        </form>


        <script type="text/javascript">

            var facebookApplicationID = '13xxxxxxxxxxxxx'; // edited out for this forum thread


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

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


        </script>
        <script type="text/javascript">


            var photosContainerDivID = 'facebookPhotosContainer';
            var loginButtonID = 'btnFacebookLogin';
            var dCookieValues = null;
            var accessToken = null;
            var userID = null;


            // WIRE UP EVENTS //
            $('#' + loginButtonID).click(function () 
            {
                alert("inside $('#' + loginButtonID).click(");
                LoginToFacebook();
            });

...

When I load the page I get the following errors:

FB is not defined
[Break on this error] FB.getLoginStatus(function (response)
FacebookAPI.js (line 195)

I've loaded the Facebook JS SDK in the what is this a callback?

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

not sure what I'm missing here.

UPDATED:

        var facebookApplicationID = 'xxxxxxxxxxxxxxxx;


        window.fbAsyncInit = function () {
            alert("got here");
            // Initialize/load the JS SDK
            // cookie is set to true to activate JS SDK cookie creation & management
            FB.init({ appId: facebookApplicationID, status: true, cookie: true, xfbml: false });

            // now that the Facebook JS SDK is Initialized, continue with core logic
            FacebookJsSdkInitializedCallback();

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


    </script>

    <form id="form1" runat="server">
        <div id="facebookPhotos-iFrameContent">
            <div>
                <p>Log into facebook by clicking on the button below</p>
                <p id="facebook-LoginButtonContainer">

                <%--<asp:ImageButton ID="btnFacebookLogin" runat="server" ImageUrl="images/facebookLoginBtn.jpg" />--%>
                <input type="image" id="btnFacebookLogin" src="images/facebookLoginBtn.jpg" />

                </p>
                <p><asp:DropDownList ID="facebookAlbumDropdown" runat="server" /></p>
                <div id="facebookPhotosContainer" />
            </div>
        </div>

        <div id="fb-root"></div>

    </form>

    <script type="text/javascript">

        var photosContainerDivID = 'facebookPhotosContainer';
        var loginButtonID = 'btnFacebookLogin';
        var dCookieValues = null;
        var accessToken = null;
        var userID = null;

        function FacebookJsSdkInitializedCallback()
        {
            // bind the login button
            $('#' + loginButtonID).click(function () {
                alert("inside $('#' + loginButtonID).click(");
                LoginToFacebook();
            });

        }

I'm getting these errors now and I still don't know why. I've obviously now added a callback so I know that the Init is done. But I the code that I believe is loading the library into the page is failing:

$("fb-root").appendChild is not a function
[Break on this error] $('fb-root').appendChild(e);

So it's prob because the jQuery library was not loaded yet but this async method ran before it was loaded. So not sure then how to handle this. You can see my jQuery includes at the top.

+3  A: 

As you are using async loading method for FB API, it is not getting loaded when you are calling it few lines later. You have two options:

1) Switch to synchronous loading method (see first example on this page)

2) Add some callback to window.fbAsyncInit() function so you know when API is loaded and ready to use:

window.fbAsyncInit = function () {
    FB.init({ appId: facebookApplicationID, status: true, cookie: true, xfbml: false});
    fbInitializedCallback();
}

function fbInitializedCallback()  {
    // WIRE UP EVENTS //
    ....
    LoginToFacebook();
}  

BTW if you choose to stay with async method you should really put that code right after opening <body> tag, it wouldn't slow down your page load.

serg
ok so you're saying use the callback...then you know the library is loaded...got it. Makes sense. I figured that my binding of the Login function to my button was already being run after the async was done but apparently no, the callback tells you it's definitely done which make obvious sense. So my existing code was probably trying to call FB methods as you are saying before the async was done...which the callback would tell you it's done.
CoffeeAddict
why does it matter where I put this async script, I don't get why in regards to how the page loads.
CoffeeAddict
@coffeeaddict: Async loader loads that script in parallel with the rest of the page. So the earlier you put in on the page the earlier it would start loading (and as a result would complete loading faster). Putting it lower doesn't give you any advantages, only disadvantages.
serg
got it. Yea I knew it loaded parallel which is what asynchronous was all about but I did not think about the fact that it would start sooner cause I did not know what really loads first with the DOM. I guess everything just loaded in order of what's on the page. Sometimes I just tend to think there is more going on here but there's not.
CoffeeAddict
so the ;(function () { var e = document.createElement('script'); e.async = true;is not a callback? And I guess that's actually spitting the include to the .js file of the SDK on the page??
CoffeeAddict
I am not a JS guru (yet) but will be pretty quick. I just don't understand if that last function append after the closing bracket of the async is a callback or what? It starts with (function() { ...and so on and starts right after the ending }; of the window.fbAsyncInit's closing bracket so that looks a little weird to me. I've done some jQuery and this is kinda just different than I've seen before both in just plain JS or jQuery. I'm sure it's fundamental just don't recognize how that's being just called with a starting ( and ending )....
CoffeeAddict
One thing I also don't get. Check this out: http://github.com/facebook/connect-js/issues/issue/88/#comment_324269. So the example is going synchronous. But scroll down to the bottom in the comments. That guy's last example. He's not using a callback inside the async method. So I wonder why not...he's essentially doing what I was doing I think...or maybe he's not because since it's inside the async, he puts that login bind after the Init() so that he knows the Init is done and the SDK has been loaded?
CoffeeAddict
I've still bot the $("fb-root").appendChild is not a function error. I'm reposting my updated code.
CoffeeAddict
Just copy init code from this page and don't change anything: http://developers.facebook.com/docs/reference/javascript/ Why did you put jquery there for? Also `<div id="fb-root"></div>` should be above that script, just like in the example.
serg
I saw this done by another guy in terms of specifically using jQuery to ref that fb-root and I like it since I'm already using jQuery for half of the rest of my code that runs after the init anyways. I tried putting the div above but the problem remained that jQuery was not loaded therefore the error. Also why would it be having a problem finding the callback function? That may go away once I resolve the fb-root reference. I am not clear on how and when that script that runs works that takes care of wiring up the .js include I believe for //connect.facebook.net/en_US/all.js
CoffeeAddict
I'm using document.getElementById('fb-root') now but still, I should be able to use jQuery in that async wrapper though...so let me play with this. I would much rather reference things with jQuery just personal preference and it's just shorter code anyway.
CoffeeAddict
thanks for pointing out what's obvious which now seems trivial...just a lot going on here. It's totally clear...now I'm just going to try to get this jQuery library load issue resolved and I'm off.
CoffeeAddict