Skip to content

Instantly share code, notes, and snippets.

@jacquerie
Last active August 29, 2015 14:00
Show Gist options
  • Save jacquerie/11189438 to your computer and use it in GitHub Desktop.
Save jacquerie/11189438 to your computer and use it in GitHub Desktop.
Turbolinks.js + iubenda.js
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