Skip to content

Instantly share code, notes, and snippets.

View chrisjhoughton's full-sized avatar

Chris Houghton chrisjhoughton

  • Beacon
  • London, UK
View GitHub Profile
@chrisjhoughton
chrisjhoughton / snippet.js
Last active January 2, 2016 07:39
The Sauce JavaScript snippet.
window.Sauce = {
isReady: false,
options: {},
object: {},
describe: function (type, data) {
this.object[type] = data;
@chrisjhoughton
chrisjhoughton / keenio-iframe.js
Created December 18, 2013 14:13
Load Keen.io JavaScript data collection SDK in a local iframe. The primary use-case for this is if you're tracking across multiple client sites and do not wish to pollute global name-spacing.
// Sample options to pass to Keen.configure
// {
// projectId: "your_project_id",
// writeKey: "your_write_key",
// }
var loadKeen = function (options, cb) {
var iframe = document.createElement("iframe");
iframe.style.display = "none";
@chrisjhoughton
chrisjhoughton / indexOf.js
Created December 10, 2013 13:07
Cross-browser compatible indexOf
var indexOf = function(arr, item, fromIndex) {
fromIndex = fromIndex || 0;
if (arr === null) {
return -1;
}
var len = arr.length,
i = fromIndex < 0 ? len + fromIndex : fromIndex;
while (i < len) {
// we iterate over sparse items since there is no way to make it
@chrisjhoughton
chrisjhoughton / wait-el.js
Last active December 2, 2025 14:18
Wait for an element to exist on the page with jQuery
var waitForEl = function(selector, callback) {
if (jQuery(selector).length) {
callback();
} else {
setTimeout(function() {
waitForEl(selector, callback);
}, 100);
}
};
@chrisjhoughton
chrisjhoughton / average.js
Created December 10, 2013 13:05
Very basic max, min, average functions. All depend on forEach: https://gist.github.com/chrisjhoughton/7890274
var average = function (arr) {
var total = 0;
forEach(arr, function (number) {
total += number;
});
return total / arr.length;
};
@chrisjhoughton
chrisjhoughton / foreach.js
Created December 10, 2013 13:04
Foreach from MoutJS. Never write a for loop again!
var forEach = function(arr, callback, thisObj) {
if (arr === null) {
return;
}
var i = -1,
len = arr.length;
while (++i < len) {
// we iterate over sparse items since there is no way to make it
// work properly on IE 7-8. see #64
if (callback.call(thisObj, arr[i], i, arr) === false) {
@chrisjhoughton
chrisjhoughton / cookie.js
Last active December 30, 2015 21:50
Generic cookie functions
@chrisjhoughton
chrisjhoughton / wait-global.js
Created December 10, 2013 13:02
Wait for a global variable to exist on the page.
var waitForGlobal = function(key, callback) {
if (window[key]) {
callback();
} else {
setTimeout(function() {
waitForGlobal(key, callback);
}, 100);
}
};
var ucFirst = function (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
};
@chrisjhoughton
chrisjhoughton / querystring.js
Created September 30, 2013 14:53
A module to get a query parameter
define(function() {
var QueryString = function() {
// This function is anonymous, is executed immediately and
// the return value is assigned to QueryString!
var query_string = {};
var query = window.location.search.substring(1);
// String trailing forward slash if exists
if (query.charAt(query.length - 1) == "/") {