Skip to content

Instantly share code, notes, and snippets.

@acoyfellow
Last active October 28, 2017 21:13
Show Gist options
  • Save acoyfellow/6d2f7c39723e4053f40b76f58ee53dba to your computer and use it in GitHub Desktop.
Save acoyfellow/6d2f7c39723e4053f40b76f58ee53dba to your computer and use it in GitHub Desktop.
Be sure to modify YOUR_WEBSITE_ID on line 34
/*
* Usage
* This function will append an external OptKit JavaScript to the head of the document.
*
* @param {String} location - The location of the file you'd like to load.
* @param {String} website_id - The OptKit website ID you'd like to load on this variation.
* @param {Function} callback - [OPTIONAL] A function to call when the script has completed downloading.
*
*/
var loadOptKit = function(location, website_id, callback){
window.OptKit_ID= website_id;
var fileRef = document.createElement('script');
fileRef.setAttribute('type','text/javascript');
if (callback) {
if (fileRef.readyState) { // IE
fileRef.onreadystatechange = function() {
if (fileRef.readyState == 'loaded' || fileRef.readyState == 'complete') {
fileRef.onreadystatechange = null;
callback();
}
};
} else { // Non-IE
fileRef.onload = function(){
callback();
};
}
}
fileRef.setAttribute('src', location);
document.head.appendChild(fileRef);
};
loadOptKit('https://go.optkit.com/widget/load.js', 'YOUR_WEBSITE_ID', function() {
// CALLBACK - code that does something after OptKit has been injected.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment