Search code examples
javascriptfacebookfacebook-javascript-sdkfacebook-sharer

Facebook post to newsfeed dont show FB is not defined


Im trying to post to facebook newsfeed and the popupbox doesn't show up from time to time. I think it has to do with postbacks or something. This is whats happen: The users bye something and get send to Payson (payson is Swedish version of Paypal) in a popup window and then payson send the user back to my page still in the popup window. This is a code that runs:

function postToFeed() {

try {
    FB.getLoginStatus(function (response) {
        if (response.status === 'connected') {

            var obj = {
                method: 'feed',
                link: 'link',
                picture: 'imagelink',
                name: 'title',
                caption: 'some text...',
                description: 'some text...',
                redirect_uri: 'url'
            };

            function callback(response) {
                document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
            }

            FB.ui(obj, callback);
        }
        else {
            alert("not connected")
        }
    });

} catch (e) {
    alert(e.Message)
}
 }

Now whats happen is that i get the error from my try catch and it's just sayin FB is not defined .

If i run this function with a button afterwards it's working just fine. If i fake the postback from payson it's also working, but not if i do it in a new window.

I tried to put the FB.init function in my function but that didn't help.

Thanks for your help!

New Code

This is in a .js file

var isLoaded = false;
window.fbAsyncInit = function () {
FB.init({
    appId: '00000000', // App ID
    channelUrl: 'url', // Channel File
    status: true, // check login status
    cookie: true, // enable cookies to allow the server to access the session
    xfbml: true,  // parse XFBML
    oauth: true // enable OAuth
});

isLoaded = true;
 };

// Load the SDK Asynchronously
  (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/en_US/all.js";
ref.parentNode.insertBefore(js, ref);
   } (document));

   (function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/sv_SE/all.js#xfbml=1&appId=503537396325197";
fjs.parentNode.insertBefore(js, fjs);
 } (document, 'script', 'facebook-jssdk'));


FB.init({ appId: "000", status: true, cookie: true });


   function postToFeed() {

    var obj = {
        display: 'iframe',
        method: 'feed',
        link: 'url',
        picture: 'url',
        name: 'text',
        caption: 'text',
        description: 'text',
        redirect_uri: 'url'
    };

    function callback(response) {
        document.getElementById('msg').innerHTML = "Post ID: " + response['post_id'];
    }

    FB.ui(obj, callback);
   }


   function init() {
    FB.init({
    appId: '00000',
    status: true, // check login status
    cookie: true, // enable cookies to allow the server to access the session
    xfbml: true, // parse XFBML
    channelUrl: 'url', // custom channel
    oauth: true // enable OAuth
     });

    FB.getLoginStatus(function (response) {
    if (response.status == 'connected') {
        var user_id = response.authResponse.userID;
        var access_token = response.authResponse.accessToken
        var page_id = "00000"; //page id
        var fql_query = "SELECT uid FROM page_fan WHERE page_id =" + page_id + " and uid=" + user_id;
        var the_query = FB.Data.query(fql_query);

        the_query.wait(function (rows) {

            if (rows.length == 1 && rows[0].uid == user_id) {
                $("#container").show();
                $("#container_notlike").hide();
                $("#notloggedin").hide();

            }

            else {
                $("#container_notlike").show();
                $("#container").hide();
                $("#notloggedin").hide();

                login();
            }
        });
    } else {
        $("#container_notlike").hide();
        $("#container").hide();

        login();

    }
     });

     }

      init();

       // whenever the user logs in, we refresh the page
     FB.Event.subscribe('auth.login', function (response) {
     top.location.href = "url";
   });


    FB.Event.subscribe('auth.logout', function (response) {
      top.location.href = "url";
     });




      function login() {
     FB.login(function (response) {
        if (response.authResponse) {
        console.log('Logged in');
        } else {
        console.log('User cancelled login or did not fully authorize.');
        }
       }, { scope: 'email,user_likes,publish_actions' });
       }



    function likereload() {

    FB.Event.subscribe('edge.create', function (response) {

      top.location.href = "url"
       });

   }


    function getuserinfo() {

       FB.getLoginStatus(function (response) {
          if (response.authResponse) {

           FB.api('/me', function (response) {
            document.getElementById("txtfName").value = response.first_name;
            document.getElementById("txtlName").value = response.last_name;
            document.getElementById("txtemail").value = response.email;

            });

         }
      });


       }

Solution

  • I solved the problem,

    window.setTimeout('postToFeed()', 3000);
    

    Then all facebook code have time to load before i bring up the feed dialog