Topic-icon Uncaught ReferenceError: FB is not defined

Active Subscriptions:

None
Hi,

Am testing instagram api javascript functions which uses the facebook javascript library to implement on the webpage. But my code below won't work.

<SCRIPT type="text/javascript">
   /* make the API call */
FB.api(
    "/oembed_page",
    function (response) {
      if (response && !response.error) {
        /* handle the result */
      }
    }
);

</SCRIPT>

Although i have it after:
window.fbAsyncInit = function() {
    FB.init....

Am i missing something?

Many thanks in advanced.
The topic has been locked.
Support Specialist
1 year 9 months ago #68135 by mel
The Facebook Javascript library is loaded asynchronously, which means it probably hasn't been fully loaded by the time you make your call.

You should wrap your call in an onReady handler so that your code only executes after the browser says it's done loading all scripts, like:
jQuery(document).ready(function(){
  // DO SOMETHING HERE
});

I hope that helps get you going!
The topic has been locked.
Active Subscriptions:

None
Hi Mel,

Thank you for your quick response. I did already try your suggestion. Which i should of mentioned before. Do you have any other ideas?
The topic has been locked.
Support Specialist
1 year 9 months ago #68141 by alzander
Can you let us know a URL we can test on? We know the domain, so you don't need to post that if you'd like to keep your site private, but we'll need a path to a page where you're testing so we can see the Javascript error and understand how you've implemented the code.

We just tested on your home page and I didn't see any calls to FB and no Javascript errors either.

Thanks,
Alex
The topic has been locked.
Active Subscriptions:

None
Hi Alex,

I've solved the issue now. Because FB.api calls have to be run inside fbAsyncInit. i have added a queue processor to your function. So that i can run fb.api calls. Not the cleanest way of doing things. But i don't thing their are any alternatives, unless you add a queue function to your fb initialize function in the future?

Here where i got the code from.

www.faqcode4u.com/faq/554816/how-to-call...e-window-fbasyncinit

If you have a more elegant solution, then please let me know.

Thank you,

Dowayne
The topic has been locked.
Support Specialist
1 year 9 months ago #68169 by alzander
I couldn't see the solution code on that page, so I can't be sure, but what you describe is a valid solution. Basically, you can't make any FB calls until it's been loaded. If you implemented a method to chain/queue your call until after the init, that will get the job done.

Facebook does have some suggestions for how to do this best, but it's more of a handwaving of 'make sure FB is loaded first'. If you don't want your queue method, you can do the jQuery ready method and then do a quick polling to check if FB is available every 100ms and then execute the code once FB is ready. That may be very similar to what you're doing now.

Neither option is elegant, but hope that helps,
Alex
The topic has been locked.