Skip to content

Instantly share code, notes, and snippets.

@carlsverre
Created May 19, 2011 02:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlsverre/980064 to your computer and use it in GitHub Desktop.
Save carlsverre/980064 to your computer and use it in GitHub Desktop.
How I didn't destroy the internet
// callback is a function that transitions to the new url
// the setup below ensures that we call the callback as soon as we hear
// back from mixpanel, or if 300 ms has passed, whichever happens first.
var t = window.setTimeout(callback, 300);
var cb = function() {
// we need to clear the timeout if we hear back from mixpanel
// otherwise we could trigger two page loads
window.clearTimeout(t);
callback();
};
// we send the link href along with the event
if(typeof(properties.url) === 'undefined' && element.href) {
properties.url = element.href;
}
metrics.track(event_name, properties, cb);
// prevent_default() ensures that the browser doesn't fire the default
// click event (or it would go to the page without waiting for mixpanel)
return prevent_default(e);
function to_array(obj) {
var l, i = 0, ret = [];
if(obj === null || obj === undefined) { return []; }
try {
return Array.prototype.slice.call(obj, 0);
} catch (err) {
if (typeof(obj.length) === "number") {
for (l = obj.length; i < l; i++) {
ret[i] = obj[i];
}
} else {
while (obj[i] !== undefined) {
ret[i] = obj[i];
i++;
}
}
return ret;
}
}
function get_elements_by_class_name(className) {
if (typeof(document.getElementsByClassName) === "undefined") {
var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
var allElements = document.getElementsByTagName("*");
var results = [];
var element, i;
for (i = 0; (element = allElements[i]) != null; i++) {
var elementClass = element.className;
if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass)) {
results.push(element);
}
}
return results;
} else {
var elements = document.getElementsByClassName(className);
return to_array(elements);
}
}
function get_element_by_id(q) {
if (typeof(q) !== 'string') { return q; }
if (document.getElementById === undefined) {
if (document.all !== undefined) { return document.all[q]; }
if (document.layers !== undefined) { return document.layers[q]; }
return null;
} else {
return document.getElementById(q);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment