Skip to content

Instantly share code, notes, and snippets.

@cowboy
cowboy / dump-cssRules.js
Created July 7, 2010 13:40
Dump all document.styleSheets[*].cssRules[*].cssText to console
// Dump all document.styleSheets[*].cssRules[*].cssText to console:
(function(s,c,r,i,j){ for(i=0;i<s.length;i++){c=s[i].cssRules;for(j=0;j<c.length;j++){r=c[j].cssText;console.log('document.styleSheets['+i+'].cssRules['+j+'].cssText = "'+r+'";')}} })(document.styleSheets);
@cowboy
cowboy / jquery-pluginization.js
Created July 12, 2010 17:36
jQuery Pluginization (see comment for slide order)
(function($){
// Store fetched long URLs here.
var cache = {};
$.longUrl = function( url, callback, options ) {
// Override defaults with specified options.
options = $.extend( {}, $.longUrl.options, options );
var params = {};
@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 = {};
@cowboy
cowboy / queueing-article-idea.js
Created July 13, 2010 18:51
Queuing article code idea
// I need to explain this. Soon.
// BAD
function a(){
console.log(1);
b();
};
function b(){
@cowboy
cowboy / jquery.ba-simple-cache.js
Created July 15, 2010 16:58
jQuery memoization, of sorts. WIP UNTESTED
/*!
* $imple cache - v0.3pre - 07/16/2010
* http://benalman.com/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function($,window){
/*
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.
@cowboy
cowboy / replacetext-textarea.js
Created July 23, 2010 14:47
jQuery replaceText - textarea support (?)
// If you want jQuery replaceText to be able to replace modified content
// inside a textarea, you might need to do this kind of thing, since
// changing the textarea .value doesn't change the textarea's child text
// node!
$('textarea')
.each(function(){
$(this).html( this.value ).val( this.value );
})
function filter( e ){
var term = $.trim( this.value.toLowerCase() );
if( !term ){
rows.show();
} else {
rows.hide();
self._trigger( "filter", e, $.map( cache, function(v,i){
if ( v.indexOf(term) !== -1 ) {
// plugin "foo plus stuff" contains standalone plugin "foo"
// additional code
(function($){
$.foo = {};
$.foo.bar = prop;
})(jQuery);
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) {