Reading from Facebook docs seems to be very easy to embed videos on Facebook. However, the <div id="fb-root"></div> should be immediately before the div script and fb-video class (from what I observed). Therefore, if I want to embed a video, I should do something like:
<div className="card-video-wrapper"> <div id="fb-root"></div> <script>(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/es_ES/sdk.js#xfbml=1&version=v2.8"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script> <div className="fb-video" data-href="my-video-url" data-show-text="false" data-autoplay="true"></div> </div>
First, I want <div id="fb-root"></div> and the script to go right after the open tag, but this way the video is hiding at the top of the page.
The problem occurs when I want to embed several videos on Facebook, for example:
<div className="card-video-wrapper"> <div id="fb-root"></div> <script>(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/es_ES/sdk.js#xfbml=1&version=v2.8"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script> <div className="fb-video" data-href="my-video-url-1" data-show-text="false" data-autoplay="true"></div> </div> <div className="card-video-wrapper"> <div id="fb-root"></div> <script>(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/es_ES/sdk.js#xfbml=1&version=v2.8"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script> <div className="fb-video" data-href="my-video-url-2" data-show-text="false" data-autoplay="true"></div> </div>
In this case, the second is not displayed.
The <div id="fb-root"></div> is probably placed once, but, as I said, the video is hidden. Consider an example:
<body> <div id="fb-root"></div> <script>(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/es_ES/sdk.js#xfbml=1&version=v2.8"; fjs.parentNode.insertBefore(js, fjs); }(document, 'script', 'facebook-jssdk'));</script> ... <div className="card-video-wrapper"> <div className="fb-video" data-href="my-video-url-1" data-show-text="false" data-autoplay="true"></div> </div> <div className="card-video-wrapper"> <div className="fb-video" data-href="my-video-url-2" data-show-text="false" data-autoplay="true"></div> </div> ... </body>
Checking using the navigator console I see that these styles apply to the children of the fb-root layer:
position: absolute; top: -10000px; height: 0px; width: 0px;
How can I embed multiple videos on Facebook?
Edit:
You might think that this should be related to CSS. However, even deleting all CSS rules, I see the same problem, videos are not displayed (even one video) when <div id="fb-root"></div> and script are placed after the source tag.
On the other hand, I tested it with a simple page (as @hackerrdave suggested) and it works. Now I am wondering what could be the problem. This is a React application that uses some components of Framework7 (I'm not sure if this may be relevant).