facebook using jquerymobile frame work-Collection of common programming errors

I am trying to implement the basic features of facebook like updating the facebook profile, and also facebook login using jquerymobile frame work. Using this link http://thinkdiff.net/facebook/graph-api-javascript-base-facebook-connect-tutorial/

It works fine with HTML 5 format. But when I integrate with the JqueryMobile, I am getting an error which says Uncaught exception: cant call method appendChild.

I will paste the code here: please have a look and let me know whats the problem.

 

window.fbAsyncInit = function() {

                FB.init({appId: 'xxxxxxxxxxxxxx', status: true, cookie: true, xfbml: true});



                /* All the events registered */

                FB.Event.subscribe('auth.login', function(response) {

                    // do something with response

                    login();

                });

                FB.Event.subscribe('auth.logout', function(response) {

                    // do something with response

                    logout();

                });



                FB.getLoginStatus(function(response) {

                    if (response.session) {

                        // logged in and connected user, someone you know

                        login();

                    }

                });

            };

            (function() {

                var e = document.createElement('script');

                e.type = 'text/javascript';

                e.src = document.location.protocol +

                    '//connect.facebook.net/en_US/all.js';

                e.async = true;

                document.getElementById('fb-root').appendChild(e); //i am getting error in the is line

            });



            function login(){

                FB.api('/me', function(response) {

                    document.getElementById('login').style.display = "block";

                    document.getElementById('login').innerHTML = response.name + " succsessfully logged in!";

                });

            }

            function logout(){

                document.getElementById('login').style.display = "none";

            }



            //stream publish method

            function streamPublish(name, description, hrefTitle, hrefLink, userPrompt){

                FB.ui(

                {

                    method: 'stream.publish',

                    message: '',

                    attachment: {

                        name: name,

                        caption: '',

                        description: (description),

                        href: hrefLink

                    },

                    action_links: [

                        { text: hrefTitle, href: hrefLink }

                    ],

                    user_prompt_message: userPrompt

                },

                function(response) {



                });



            }

            function showStream(){

                FB.api('/me', function(response) {

                    //console.log(response.id);

                    streamPublish(response.name, 'Something ', 'hrefTitle', 'http://www.ffff.com', "Share www.ffffff.com");

                });

            }



            function share(){

                var share = {

                    method: 'stream.share',

                    u: 'http://www.fffffff.com'

                };



                FB.ui(share, function(response) { console.log(response); });

            }



            function graphStreamPublish(){

                var body = 'hsdfkjasdkjfadkjf;adlfj';

                FB.api('/me/feed', 'post', { message: body }, function(response) {

                    if (!response || response.error) {

                        alert('Error occured');

                    } else {

                        alert('Post ID: ' + response.id);

                    }

                });

            }



            function fqlQuery(){

                FB.api('/me', function(response) {

                     var query = FB.Data.query('select name, hometown_location, sex, pic_square from user where uid={0}', response.id);

                     query.wait(function(rows) {



                       document.getElementById('name').innerHTML =

                         'Your name: ' + rows[0].name + "
" + '' + "
"; }); }); } function setStatus(){ status1 = document.getElementById('status').value; FB.api( { method: 'status.set', status: status1 }, function(response) { if (response == 0){ alert('Your facebook status not updated. Give Status Update Permission.'); } else{ alert('Your facebook status updated'); } } ); }

Foofys-Facebook Page

    

       
        
you are using foofys facebook app
Publish Wall Post | Publish Stream | Write your status here'



Page Footer

    



i am not able to understand whats happening in the code, BTW I have just pointed where exactly I am getting the error.

  1. The trick with JQM and FB api is to use the graph API. That is DO NOT use the simple javascript FB wrappers since they are unstable when exposed to JQM's page handling - instead just use the new graph / rest API, check for and avoid multiple inits of the FB core and your'e set. For instance

    function updateUserInfo(uid, accessToken) {
    
        var uri = "https://graph.facebook.com/" + uid;
        console.log("About to call FP.api with URI " + uri);    
    
        $.ajax({
                    type: "GET",
                    url: "https://graph.facebook.com/" + uid,
                    dataType: "json",
                    success: 
                        (function (response) {
                            console.log("About to call check profile ...");
                            $("#p_name").val(response.name);
                            $("#email").val(response.email);
                            $("#fb_id").val(response.id);
                            $.ajax({
                                type: "POST",
                                url: "/check_profile",
                                cache: false,
                                data: {fb_id: response.id},
                                success: onCheckSuccess,
                                error: onError
                            });
                            console.log("FB id: " + response.id);                     
                        }),
                    error: onError
             });
    

Originally posted 2013-11-27 12:01:26.