Skip to content

Instantly share code, notes, and snippets.

@6temes
Last active October 22, 2019 18:38
Show Gist options
  • Save 6temes/52648dc6b3adbbf05da3942794b97a00 to your computer and use it in GitHub Desktop.
Save 6temes/52648dc6b3adbbf05da3942794b97a00 to your computer and use it in GitHub Desktop.
Compatibility between Facebook Plugin and Turbolinks 5
// FacebookSDK
// https://developers.facebook.com/docs/plugins/page-plugin/
(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_US/sdk.js#xfbml=1&version=v2.8";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk')); // Replace 'facebook-jssdk' with your page id.
// Compatibility with Turbolinks 5
(function($) {
var fbRoot;
function saveFacebookRoot() {
if ($('#fb-root').length) {
fbRoot = $('#fb-root').detach();
}
};
function restoreFacebookRoot() {
if (fbRoot != null) {
if ($('#fb-root').length) {
$('#fb-root').replaceWith(fbRoot);
} else {
$('body').append(fbRoot);
}
}
if (typeof FB !== "undefined" && FB !== null) { // Instance of FacebookSDK
FB.XFBML.parse();
}
};
document.addEventListener('turbolinks:request-start', saveFacebookRoot)
document.addEventListener('turbolinks:load', restoreFacebookRoot)
}(jQuery));
@majtymit
Copy link

You dont wanna know how much time I spent trying to fix this shit...and then I found your gist <3 thank you very very much!

@matissg
Copy link

matissg commented Jan 14, 2019

Same here! Thank you - saved me ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment