Skip to content

Instantly share code, notes, and snippets.

@cowboy
cowboy / gist:303386
Created February 13, 2010 11:12 — forked from padolsey/gist:303362
var a = 1;
(function(){
var a = 2;
(function(parent){
var global = this;
function repeat(s,n){return Array(n+1).join(s);}
// usage
repeat('x', 3); // returns "xxx"
//EnhanceJS isIE test idea
//detect IE and version number through injected conditional comments (no UA detect, no need for cond. compilation / jscript check)
//version arg is for IE version (optional)
//comparison arg supports 'lte', 'gte', etc (optional)
var isIE = (function(){
var doc = document,
@cowboy
cowboy / gist:360138
Created April 8, 2010 14:46 — forked from remy/gist:360113
setInterval and setTimeout patterns
// =========================
// SetInterval
// =========================
// While not truly accurate, setInterval is still fairly good, time-wise.
// Better for things like a "one second tick" but not great for expensive
// code executed at small intervals as iterations can "stack".
// (ECMAScript 5 strict mode compatible)
@cowboy
cowboy / gist:379255
Created April 26, 2010 12:20 — forked from remy/gist:378764
Just another way of doing it...
// "Cheat" special event using this pattern:
// http://benalman.com/news/2010/03/jquery-special-events/#pattern
//
// Also see:
// http://benalman.com/news/2010/03/jquery-special-events/#add-and-remove-tripleclick-per-handler
(function($){
$.event.special.cheat = {
setup: function() {
@cowboy
cowboy / jquery.ba-bindandtrigger.js
Created April 30, 2010 12:49 — forked from westonruter/jquery.bindAndCall.js
Bind an event handler and fire it immediately
/*!
* bindAndTrigger - v0.1 - 04/30/2010
* http://benalman.com/
*
* http://jsfiddle.net/cowboy/fJnA2/
*/
(function($,undefined){
$.fn.bindAndTrigger = function( all, type, data, callback ) {
$.fn.format = function(opts){
var o = $.extend({}, defaults.format, opts), codez = formatCodes(o.locale);
return this.each(function(){
var me = $(this);
me[ me.is(":input") ? "val" : "text" ](function(i,v){
return formatNumber( v, o, codez );
});
});
}
@cowboy
cowboy / gist:473071
Created July 12, 2010 21:14 — forked from codylindley/gist:472928
For Cody
/*!
* jQuery.preloadImg
* description: cache images via $.preloadImg(['src','src']) or $('img').preloadImg()
* author: Cody Lindley
*/
(function($) {
// Internal cache of image src values.
var cache = {};
/*
Usages:
$(selector).classList() //returns an array of classnames
$(selector).classList('newclass') //replaces the current element's classes
$(selector).classList(['new', 'class', 'names']) //replaces the current element's classes
*/
jQuery.fn.classList = function( classNames ) {
if ( jQuery.isArray( classNames ) ) {
// An array was passed, join it into a string.
function relativeDate(str) {
var s = ( +new Date() - Date.parse(str) ) / 1e3,
m = s / 60,
h = m / 60,
d = h / 24,
w = d / 7,
y = d / 365.242199,
M = y * 12;
function approx(num) {