-
-
Save StarWar/799f5a639d80c0dd01d33d203e6c783f to your computer and use it in GitHub Desktop.
Compatibility between Facebook Plugin and Turbolinks 5
This file contains hidden or 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/ja_JP/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