Skip to content

Instantly share code, notes, and snippets.

View eriwen's full-sized avatar

Eric Wendelin eriwen

View GitHub Profile
@eriwen
eriwen / array-extras.js
Created March 5, 2011 19:40
Playing around with implementing ES5 Array extras
/**
* Attach function to Array.prototype, with the given implementation.
*/
function augmentArrayPrototypeWith(func, impl) {
if (typeof Array.prototype[func] !== 'function') {
Array.prototype[func] = function() {
return impl.apply(this, arguments);
};
}
}
@eriwen
eriwen / debug.js
Created August 27, 2011 04:40
Useful functions for web debugging (via @LeaVerou)
function $(id) { return document.getElementById(id); }
function $t(tag, con) { return (con || document).getElementsByTagName(tag); }
function $$(expr, con) { return (con || document).querySelectorAll(expr); }
@eriwen
eriwen / selector-supported.js
Created August 28, 2011 02:04
JS test for selector support (another brilliant bit from @LeaVerou)
function supportsSelector(selector) {
var el = document.createElement('div');
el.innerHTML = ['&shy;', '<style>', selector, '{}', '</style>'].join('');
el = document.body.appendChild(el);
var style = el.getElementsByTagName('style')[0],
ret = !!(style.sheet.rules || style.sheet.cssRules)[0];
document.body.removeChild(el);
return ret;
}
@eriwen
eriwen / chain.js
Created September 6, 2011 15:35
Simple function chaining with a timeout (not ES5 compatible)
Array.prototype.chain = function chain(delay) {
var tasks = this, pos = 0, delay = delay || 17;
setTimeout(function() {
tasks[pos++]();
if (pos < tasks.length) setTimeout(arguments.callee, delay);
}, delay);
return this;
};
// Usage;
@eriwen
eriwen / memoize.js
Created September 6, 2011 15:38
General purpose memoization
/**
* A general-purpose function to enable a function to use memoization. The function must use explicit, string-serializable parameters
* @param func {Function} to be memoized
* @parma context {Object} the context for the memoized function to execute within
*/
function memoize(func, context) {
function memoizeArg (argPos) {
var cache = {};
return function() {
if (argPos == 0) {
@eriwen
eriwen / viewport-size.js
Created September 6, 2011 15:41
Get viewport dimensions for all browsers
function getViewportSize() {
var windowWidth = document.documentElement.clientWidth || document.body.clientWidth;
var windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
return [windowWidth, windowHeight];
}
@eriwen
eriwen / override-inline.css
Created September 6, 2011 15:43
How to override inline CSS styles (IE7+) - use very sparingly!
span[style] {
/* inheritable-property: inherit !important; */
color: inherit !important;
font-size: inherit !important;
}
@eriwen
eriwen / schedule.js
Created September 6, 2011 15:46
Function chaining without Array prototype modifications
/**
* Function chaining without modifying Array prototype. Not ES5 compatible.
* @param functions {Array{Function}} functions to schedule
* @param context {Object} for execution
*/
function schedule(functions, context){
setTimeout(function(){
var process = functions.shift();
process.call(context);
@eriwen
eriwen / browser-target.css
Created September 6, 2011 15:49
CSS Browser targeting
/* IE6 and below & Mac IE */
* html selector{ prop: value; }
/* IE6 and below */
/*\*/
* html selector{ prop: value; }
/**/
/* IE7 */
*:first-child+html selector{ prop: value; }
*+html selector { prop: value; }
/* Mac IE only */
@eriwen
eriwen / ftp_get.sh
Created September 6, 2011 16:07
Get file from FTP
#!/bin/sh
HOST='some.ftp.server'
USER='myuser'
PASSWD='mypass'
FILE='myfile'
ftp -n $HOST <<END_SCRIPT
user ${USER} ${PASSWD}
cd /path/to/something