Last active
August 29, 2015 14:00
-
-
Save jacquerie/11189438 to your computer and use it in GitHub Desktop.
Turbolinks.js + iubenda.js
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
class @Iubenda | |
@load: -> | |
if typeof Turbolinks isnt "undefined" and Turbolinks.supported | |
document.addEventListener "page:change", (-> | |
Iubenda.loadTag() | |
), true | |
else | |
Iubenda.loadTag() | |
@loadTag: -> | |
# Each time the page changes Iubenda adds a script tag of its code and an | |
# empty style block. We delete these before running Iubenda again. | |
Iubenda.deleteTag tag for tag in document.getElementsByTagName("script") | |
Iubenda.deleteStyle style for style in document.getElementsByTagName("style") | |
Iubenda.writeTag() | |
@writeTag: -> | |
s = document.createElement("script") | |
s.src = Iubenda.url() | |
tag = document.getElementsByTagName("script")[0] | |
tag.parentNode.insertBefore s, tag | |
@deleteTag: (tag) -> | |
if typeof tag isnt "undefined" and tag.src is Iubenda.url() | |
tag.parentNode.removeChild(tag) | |
@deleteStyle: (style) -> | |
if style.innerHTML is "" | |
style.parentNode.removeChild(style) | |
@url: -> | |
document.location.protocol + "//cdn.iubenda.com/iubenda.js" | |
Iubenda.load(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment