Skip to content

Instantly share code, notes, and snippets.

@jaimeiniesta
Created December 21, 2014 15:48
Show Gist options
  • Save jaimeiniesta/29879e7adfbadfe7b9ea to your computer and use it in GitHub Desktop.
Save jaimeiniesta/29879e7adfbadfe7b9ea to your computer and use it in GitHub Desktop.
How to load SumoMe script in a turbolinks-friendly way
# Defines a namespace for my application
MyApplication = {}
# A way to pass a callback both to jQuery's "ready" and turbolinks' "page:load" events
MyApplication.onDocumentReady = (callback) ->
$(document).ready(callback)
$(document).on('page:load', callback)
# Defines a function to load the SumoMe script
MyApplication.load_sumome_script = (data_sumo_site_id) ->
sumo = document.createElement("script")
sumo.type = "text/javascript"
sumo.async = true
sumo.src = "//load.sumome.com/"
sumo.setAttribute('data-sumo-site-id', data_sumo_site_id)
(document.getElementsByTagName("head")[0] or document.getElementsByTagName("body")[0]).appendChild sumo
# Hooks it up
MyApplication.onDocumentReady ->
MyApplication.load_sumome_script("your-site-id-here")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment