Last active
December 2, 2018 08:13
-
-
Save fongfan999/eb9db77dca53006f93f85c570d056a5f to your computer and use it in GitHub Desktop.
Turbolinks 5 & Facebook SDK & ES6
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
(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 = "https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.1&appId=YOUR_APP_ID&autoLogAppEvents=1"; | |
fjs.parentNode.insertBefore(js, fjs); | |
}(document, "script", "facebook-jssdk")); | |
document.addEventListener("DOMContentLoaded", () => { | |
let fbRoot; | |
const detatch = (elem) => { | |
return elem.parentElement.removeChild(elem); | |
} | |
const saveFacebookRoot = () => { | |
if (document.getElementById("fb-root")) { | |
fbRoot = detatch(document.getElementById("fb-root")); | |
} | |
}; | |
const restoreFacebookRoot = () => { | |
if (fbRoot != null) { | |
if (document.getElementById("fb-root")) { | |
document.getElementById("fb-root").replaceWith(fbRoot); | |
} else { | |
document.body.appendChild(fbRoot); | |
} | |
} | |
if (typeof FB !== "undefined" && FB !== null) { // Instance of FacebookSDK | |
FB.XFBML.parse(); | |
} | |
}; | |
document.addEventListener("turbolinks:request-start", saveFacebookRoot) | |
document.addEventListener("turbolinks:load", restoreFacebookRoot) | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment