Skip to content

Instantly share code, notes, and snippets.

View joshrhoades's full-sized avatar

Josh Rhoades joshrhoades

View GitHub Profile
@joshrhoades
joshrhoades / classAddRemoveCheck.js
Last active December 16, 2015 16:39 — forked from jelmerdemaat/gist:4107273
Functionality for checking and manipulating classes on DOM Elements (i.e., checking if an element has a class, removing a class from an element, and adding a class to an element. Updated to use better naming conventions, use array joins instead if string concats (operation speed), and boolean returns to more easily be able to tell when functions…
/**
* @file Functionality for checking and manipulating classes on DOM Elements (i.e., checking if an element has a class,
* removing a class from an element, and adding a class to an element. Updated to use better naming conventions,
* use array joins instead if string concats (operation speed), and boolean returns to more easily be able to tell when
* functions performed successfully within an app
* @author Josh Rhoades <http://joshuarhoades.com/>
* @added 04/25/13
* @version 1.0.0
* @see {@link https://gist.github.com/jelmerdemaat/4107273|Original before fork}
* @see {@link http://www.avoid.org/?p=78}
@joshrhoades
joshrhoades / getQueryString.js
Last active December 16, 2015 21:59
This function will qet all queryString elements available by name (key), if no matches are found, it returns `FALSE`. Revised for performance, drastically faster operation now, and using dependency injection for unit testing abilities. Typically this should be stored in a sub-namespace like `.utils`
/**
* This function will qet all queryString elements available by name (key), if no matches are found, it returns `FALSE`. Revised 3/18/13 for performance,
* drastically faster operation now, and using dependency injection for unit testing abilities. Typically this should be stored in a sub-namespace like `.utils`
* @version 0.0.3
* @method
* @name getQueryString
* @param {string} key - queryString parameter to match on
* @param {object} theContext The object to fetch against, allows for dependency injection/testability. If not passed in, function will default to use
* the global `window` object
* @returns {boolean} Returns `FALSE` if no match
@joshrhoades
joshrhoades / prototypeShims.js
Created May 2, 2013 17:28
Common and useful Prototype Shims
/**
* SHIM to add `Date.now` functionality if it is not available, based off of EcmaScript 5.
* The `Date.now()` function returns a `number` value that is the time value designating the UTC date and time of the occurrence of the call to `now`.
* @see {@link http://es5.github.com/#x15.9.4.4}
* @global
* @method
* @name Date.now
* @example var dtNow = Date.now();
* @returns {date} Number value that is the time value designating the UTC data dn time of the time of the call to this
*/
@joshrhoades
joshrhoades / logUncaught.js
Created May 9, 2013 00:28
JS global function to log all uncaught exceptions/errors, with optional method (stub) to fire and log to a server.
var arrErrors = [];
window.onerror = function(msg, fileURL, lineNum) {
arrErrors.push({ msg: msg, file: fileURL, line: lineNum });
};
setInterval(function() {
sendToServer(arrErrors);
arrErrors = [];
}, 5000);
sendToServer(arrErrors) {
@joshrhoades
joshrhoades / handlebars.getTemplate.js
Last active December 18, 2015 17:28
Function to expose precompiled handlebars as part of the handlebars object to make it easier to use HB in both DEV (runtime compile) and PROD (build/release-time compile)
Handlebars.getTemplate = function(name) {
if (Handlebars.templates === undefined || Handlebars.templates[name] === undefined) {
$.ajax({
url : 'templatesfolder/' + name + '.handlebars',
success : function(data) {
if (Handlebars.templates === undefined) {
Handlebars.templates = {};
}
Handlebars.templates[name] = Handlebars.compile(data);
},
@joshrhoades
joshrhoades / noCallbackInstantExecute.js
Created June 21, 2013 19:53
Make a dynamically injected function always execute, without the delay of callbacks or the evil of `eval`. Amazingly simple solution that works like a charm. I needed this because I wanted to make dynamically precompiled templates instantly available (in PROD mode they are compiled into a JS file, and injected if a module on the page requires it…
/*
using jQuery AJAX as an example, though we do not leverage jQuery in our Production app.
This technique can be used anywhere with injected data/functionality
*/
$.ajax({
url: theTemplate.filePath,
success: function(data) {
/*
Take the raw data of the file, insert it into a new function, and execute it.
Bypasses need for callbacks, instantly executes it without delay, and does it without using `eval`.
@joshrhoades
joshrhoades / stripFQDN.js
Last active December 20, 2015 18:39
Remove FQDN from a string
var stripFQDN = function(theURL) {
return theURL.replace(/^.*\/\/[^\/]+/, '');
};
//_stripFQDN('https://gist.github.com/assets/application-f348fb986aecf8d3b65e959e23f6a29f.css');
//returns '/assets/application-f348fb986aecf8d3b65e959e23f6a29f.css'
var getFileExtension = function(theFile) {
return theFile.split('.').pop();
};
getFileExtension('myfile.js');//returns 'js'
@joshrhoades
joshrhoades / emptySRC.html
Created August 28, 2013 00:22
Set a blank img src for setting a background image since img.src can't be targeted directly. Disables the outline/placeholder of an img element that has no SRC value (which often is a side-effect of styling the parent element of the img with the background-image).
<img src="data:image/png;base64,R0lGODlhFAAUAIAAAP///wAAACH5BAEAAAAALAAAAAAUABQAAAIRhI+py+0Po5y02ouz3rz7rxUAOw==" />
@joshrhoades
joshrhoades / formatSeconds.js
Created January 27, 2014 21:30
Quick function to format passed in seconds and determine the weeks, days, hours, minutes, and seconds in that time. Returns an object of the values.
var _formatCount = function(sec) {
return {
w: Math.floor(sec / 86400 / 7),//weeks
d: Math.floor(sec / 86400 % 7),//days
h: Math.floor(sec / 3600 % 24),//hours
m: Math.floor(sec % 3600 / 60),//minutes
s: Math.floor(sec % 3600 % 60)//seconds
};
};