67

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.

Tinggo
  • 1,003
  • 1
  • 9
  • 17

3 Answers3

49

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>
Simon Hutton
  • 1,354
  • 1
  • 14
  • 17
42

UPDATE: Facebook no longer requires that you include <div id="fb-root"></div> in your HTML.

You can now remove it. The Facebook Javascript SDK creates it on its own, appending it to the BODY tag. No warnings are shown in the console either, as it did before.

The Facebook documentation has also been updated, no longer showing the <div id="fb-root"></div> requirement.

Old documentation for version 1.0 (shows <div id="fb-root"></div>): https://developers.facebook.com/docs/javascript/quickstart/v1.0

Current documentation for version 2.5 (no longer shows <div id="fb-root"></div>): https://developers.facebook.com/docs/javascript/quickstart/v2.5

Doug S
  • 9,265
  • 2
  • 34
  • 40
  • Except then we seem to have problems like [this](http://stackoverflow.com/questions/24019877/fb-init-function-gives-wrong-version-error). – mgalgs Sep 01 '16 at 15:44
  • @mgalgs: Reading those comments, it doesn't appear that anyone found a fix for whatever was causing the OP's issue. So I wouldn't say adding the div tag is a fix for anything. If there was an issue, it's been 2 years and I'm sure it's been resolved by Facebook. The best option is to follow their code recommendations, which do not include that div tag. – Doug S Sep 02 '16 at 03:36
  • @JoshM. Any warning you're receiving isn't related to this topic. Just follow the current code that Facebook recommends. – Doug S Dec 07 '16 at 18:02
  • @DougS ?? It's completely related. v2.8 shows the warning if you don't include the `fb-root` div. – Josh M. Dec 07 '16 at 18:03
  • @JoshM. Copy-and-paste the warning here. I suspect the warning you're referring to is completely unrelated. The Facebook code has long since done away with the `div` tag. – Doug S Dec 07 '16 at 18:09
  • 2
    @DougS See here (I wish I could format it better): facebook-sdk-debug.js:2069 The "fb-root" div has not been created, auto-creating @ facebook-sdk-debug.js:2069append @ facebook-sdk-debug.js:3237appendHidden @ facebook-sdk-debug.js:3284(anonymous function) @ facebook-sdk-debug.js:7321flush @ facebook-sdk-debug.js:3164 – Josh M. Dec 13 '16 at 03:25
  • @JoshM. That's not a "warning". That's debug info simply stating that it's auto-creating the div tag. Again, just follow the setup instructions Facebook has provided, which has done away with hard-coding that div tag. – Doug S Sep 14 '17 at 08:24
  • 1
    Seems like FB reverted this change,
    does show up in the code generated by Like Button Configurator on https://developers.facebook.com/docs/plugins/like-button/#, and the button doesn't show up without this div
    – Vivek Singh Apr 04 '20 at 17:48
-2

It gets even more simple with Turbolinks 5. You just need to add data-turbolinks-permanent=true to the fb-root div and it will be persisted between requests. No need to fiddle with the javascript.

Pierre Olivier Martel
  • 3,014
  • 4
  • 23
  • 30