Last active
October 22, 2019 18:38
-
-
Save 6temes/52648dc6b3adbbf05da3942794b97a00 to your computer and use it in GitHub Desktop.
Compatibility between Facebook Plugin and Turbolinks 5
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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!