0

To begin my page i do a check with facebook to get the login status of the user. I then create a namespace within the window object for use in other javascript files. Before I load any other javascript i need this namespace to be created, or else it will be undefined when i try to access it. Sometimes it completes first, and sometimes it does not. How can i make sure that it happens correctly every time?

.
.
.
        FB.getLoginStatus(function(response) {
          // store data on the user inside of a namespace 
          // inside of the window object so it can be accessed
          // from anywhere. Use this like '$_SESSION'
          console.log('doing it');
          window.easyUserData = {
            fbRespose: response,
            site: <?php echo '"'.$site.'"'; ?>
          };
        })
      };

      (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))
    </script>
    <!-- Load the script "js/main.js" as our entry point for require -->
    <script data-main="js/main" src="lib/Require/require.js"></script>

specifically, i need the:

<script data-main="js/main" src="lib/Require/require.js"></script>

to not happen until the facebook function is complete

2 Answers 2

1

Move the code relying on the namespace into the Facebook callback. If it's the <script data-main="js/main" src="lib/Require/require.js"></script>, create the script element dynamically:

  // ...

  FB.getLoginStatus(function(response) {
    // store data on the user inside of a namespace 
    // inside of the window object so it can be accessed
    // from anywhere. Use this like '$_SESSION'
    console.log('doing it');
    window.easyUserData = {
      fbRespose: response,
      site: <?php echo '"'.$site.'"'; ?>
    };

    // Create the script element when ready
    var script = document.createElement('script');
    script.setAttribute("data-main", "js/main");
    script.src = "lib/Require/require.js";
    document.getElementsByTagName('script')[0].parentNode.appendChild(script);
    // Or instead of `document.getElementsByTagName('script')[0].parentNode`
    // you can use `document.documentElement` or `document.getElementsByTagName('head')[0]
    // or a few others
  })
};
Sign up to request clarification or add additional context in comments.

2 Comments

sorry about that, i need the "<script data-main="js/main" src="lib/Require/require.js"></script>" to happen after the fb callback
@TroyCosentino: That was my second guess, just added. :-)
0

You could move it all to one place as TJ suggests. Or you could check to see if your variable is created before attempting to run your own code:

// First this:
FB.getLoginStatus(function(response) {
    window.easyUserData = {
        fbRespose: response,
        site: <?php echo '"'.$site.'"'; ?>
    };
});

// Later in the code:
(function () {
    var isFBLoaded = function () {
        // If FB has loaded - run our code
        if (window.easyUserData) {
            myOtherFBStuff();
        }
        // If not - check again in 500 ms
        else {
            setTimeout(isFBLoaded, 500);
        }
    };

    var myOtherFBStuff = function () {
        // do stuff here
    };

    isFBLoaded();
})();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.