Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7912981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:44:10+00:00 2026-06-03T13:44:10+00:00

From: http://developers.facebook.com/docs/guides/web/ (function(d, s, id) { var js, fjs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) {return;}

  • 0

From: http://developers.facebook.com/docs/guides/web/

(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/en_US/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

2 questions:

  1. Why is the code used to load the FB SDK inside a self-invoking closure?
  2. What part of the code actually makes the SDK load asynchronously?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:44:12+00:00Added an answer on June 3, 2026 at 1:44 pm

    The function is self calling so as to not polute the global namespace. It could have very eaisly have been written as follows:

    function loadScript(d, s, id){
        //...
    }
    loadScript(document, 'script', 'facebook-jssdk');
    

    But then you’ve got an object called loadScript floating around at the global scope level, which would conflict with any other libraries that also have a variable or function called loadScript.

    It’s asynchronous because it inserts a script element into the DOM dynamically, which is an asynchronous operation. Take a look at this article for a deeper discussion, http://friendlybit.com/js/lazy-loading-asyncronous-javascript/

    Also, that’s not a closure, as there isn’t a function inside the function. A closure is a function that maintains state of it’s environment. In any event, that distinction isn’t hugely important for this question.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I ran an example copied from: http://developers.facebook.com/docs/reference/javascript/ http://developers.facebook.com/docs/reference/javascript/FB.api/ (I only changed my APP_ID) <div
I've generate scripte to like-box from this site: http://developers.facebook.com/docs/reference/plugins/like-box/ Generated script: <div id=fb-root></div> <script>(function(d,
I am on a project that involve facebook integration. Following the http://developers.facebook.com/docs/guides/mobile i am
http://developers.facebook.com/docs/reference/plugins/like To include the popular a [Like] button from facebook, I must use the
This is the code from the canvas tutorial http://developers.facebook.com/docs/appsonfacebook/tutorial/ This gets the authorization code,
As we all know ( http://developers.facebook.com/docs/share/ ) Facebook has deprecated the share function. However,
From: http://developers.soundcloud.com/docs/api/tracks#filtering I'm not sure from the docs if this functionality is available. I
I can make a search query using these tokens here: http://developers.facebook.com/docs/api#search in the links:
I am using Facebook Javascript SDK http://developers.facebook.com/docs/reference/javascript/ to authenticate and approve my Facebook app.
I followed http://developers.facebook.com/docs/authentication and got the access_token of a user. Now, I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.