Created
December 21, 2014 15:48
-
-
Save jaimeiniesta/29879e7adfbadfe7b9ea to your computer and use it in GitHub Desktop.
How to load SumoMe script in a turbolinks-friendly way
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
# 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