Skip to content

Instantly share code, notes, and snippets.

@keithws
Created June 21, 2016 21:59
Show Gist options
  • Save keithws/87a929377f7d0250bc846212c9db5c33 to your computer and use it in GitHub Desktop.
Save keithws/87a929377f7d0250bc846212c9db5c33 to your computer and use it in GitHub Desktop.
Captora Tracking Tag which loads jQuery when necessary
<!-- Captora -->
<script>
/**
* initialize Captora
* requires jQuery, so load jQuery if not present
*/
(function initCaptora () {
"use strict";
var jQueryUrl;
jQueryUrl = "https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js";
/**
* getScript
* @param {String} url The url of the script to get
* @param {Function} callback The function to call when the script is loaded
* @return {undefined}
*/
function getScript (url, callback) {
var done, firstScriptTag, isLoaded, script;
done = false;
script = document.createElement("script");
script.async = true;
script.src = url;
if (typeof callback === "function") {
script.onload = script.onreadystatechange = function handleLoad () {
isLoaded = this.readyState.match(/^(loaded|complete)$/);
if (!done && (!this.readyState || isLoaded)) {
done = true;
callback();
return;
}
};
}
firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(script, firstScriptTag);
}
if (window.jQuery) {
getScript("https://cdn.captora.com/js/track.js");
} else {
getScript(jQueryUrl, function jQueryLoaded () {
if (window.jQuery) {
getScript("https://cdn.captora.com/js/track.js");
} else {
if (window.console) {
console.warn("Unable to load Captora tracking script because jQuery is not available.");
}
}
});
}
}());
</script>
<!-- End Captora -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment