1. ホーム
  2. javascript

[解決済み] Why we need to add <div id="fb-root"></div>

2022-02-12 12:58:20

Question

Why we need to add these pair of tags within our facebook application. What's the use of this pairs of tags. I created an application which use an apprequest, and it works good even if I didn't add these tags in front of my scripts. So I really wonder why we need to add them. Thank you.

How to solved?

It's the place holder for the Facebook javascript script to attach elements to the DOM. Without this when the referenced Facebook script is ran it has nowhere to attach elements.

You can see fb-root gets appended to as part of the initialisation.

<script type="text/javascript">
      window.fbAsyncInit = function() {
        FB.init({appId: 'xxxxxx', status: true, cookie: true,
                 xfbml: true});
      };

      (function() {
        var e = document.createElement('script'); e.async = true;
        e.src = document.location.protocol +
          '//connect.facebook.net/en_US/all.js';
        document.getElementById('fb-root').appendChild(e);
      }());
    </script>