Facebook page plugin on dynamic page

浪尽此生 提交于 2020-07-03 03:50:31

问题


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 ?


回答1:


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




回答2:


What I did was move the

<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>

To a hidden div at the end of the document. Once facebook loads it then I use javascript to clone the element to where I need it to be. This example uses jQuery. Anywhere I need the page plugin to show up I placed a

<div id="facebook-timeline"></div>

Then in the javascript that loads the page or section dynamically I run the script below

$('.fb-page').clone(true).replaceAll('#facebook-timeline');


来源:https://stackoverflow.com/questions/31493945/facebook-page-plugin-on-dynamic-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!