Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Facebook page plugin on dynamic page

How can I reload facebook page element, in one page application? https://developers.facebook.com/docs/plugins/page-plugin

If I delete facebook embedded div from DOM and add this div again, facebook plugin won't automatically reinit. I'm guessing I have to do extra javascript work, to tell plugin to reinit.

Im using code that loads facebook plugin.

<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/en_GB/sdk.js#xfbml=1&version=v2.4&appId=xxx";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

Then on home dynamic tab I'm using code to embed facebook page on my one-page app:

<div class="fb-page" data-href="https://www.facebook.com/facebook" data-small-header="false" data-adapt-container-width="true" data-hide-cover="false" data-show-facepile="true" data-show-posts="true"><div class="fb-xfbml-parse-ignore"><blockquote cite="https://www.facebook.com/facebook"><a href="https://www.facebook.com/facebook">Facebook</a></blockquote></div></div>

It loads ok at the first time, I can see embeded facebook page. But if I will change tab, and change back to home again, the facebook code doesn't execute and I can't see facebook page anymore.

At this point how can manually reload Facebook embedded page ?

like image 261
Pikachu Avatar asked Feb 09 '23 13:02

Pikachu


1 Answers

Call the following JS code

FB.XFBML.parse();

after a new content loads to make the Facebook SDK reload each

<div class="fb-page" data-href="https://www.facebook.com/facebook" data-small-header="false" data-adapt-container-width="true" data-hide-cover="false" data-show-facepile="true" data-show-posts="true"><div class="fb-xfbml-parse-ignore"><blockquote cite="https://www.facebook.com/facebook"><a href="https://www.facebook.com/facebook">Facebook</a></blockquote></div></div>

tag on the page. If you need to reload a Facebook plugins only in a certain DOM element, pass it as argument to the function:

FB.XFBML.parse(document.getElementByID('certain-plugins-holder'));

Full reference: https://developers.facebook.com/docs/reference/javascript/FB.XFBML.parse

like image 51
Finesse Avatar answered Feb 12 '23 02:02

Finesse