Skip to content

Instantly share code, notes, and snippets.

@cowboy
cowboy / iife-variations.js
Created December 24, 2010 00:14
A few variations on writing an IIFE
// Want to learn more about Immediately Invoked Function Expressions? Read
// this article first:
//
// http://benalman.com/news/2010/11/immediately-invoked-function-expression/
// Parens are used to tell the parser that it's a function expression,
// not a function declaration. If you don't explicitly tell the parser to
// expect an expression, it will throw a SyntaxError exception because it
// sees a function declaration without a name specified.
@paulirish
paulirish / gist:747835
Created December 19, 2010 23:37
`x || (x = y)` pattern for using an incoming value otherwise using some default
// from https://twitter.com/#!/LeaVerou/status/16613276313980929
// this pattern below is common for using an incoming value otherwise using some default.
/* Bad: */ x = x ? x : y; // simple ternary.
// if x is truthy lets use it.
/* Better: */ x = x || y; // logical OR.
// you could string a bunch of these easily to grab the first non-falsy value:
@cowboy
cowboy / pubsub-demo.js
Created December 16, 2010 20:04 — forked from rmurphey/pubsub-demo.js
Two approaches: "Traditional" vs Pub/Sub (via rmurphey)
// Note that this uses my Pub/Sub implementation, which is slightly different than
// phiggins' in that jQuery custom events are used, and as such the first event handler
// argument passed is the event object.
//
// jQuery Tiny Pub/Sub: A really, really, REALLY tiny pub/sub implementation for jQuery
// https://gist.github.com/661855
// The "traditional" way.
@SlexAxton
SlexAxton / load structure.js
Created December 16, 2010 19:45
not working code, just the general idea
function preload(url, loadedCb) {
var tagType = 'img' || 'object',
elem = document.createElement(elem),
correctCallback = 'onerror' || 'onload';
if (tagType === 'img') {
elem.width = '1';
elem.height = '1';
}
@cowboy
cowboy / jquery.ba-htmldoc.js
Created December 16, 2010 02:44
jQuery htmlDoc "fixer" - get HTML, HEAD, BODY in your $(html) - NEEDS TESTING
/*!
* jQuery htmlDoc "fixer" - v0.2pre - 8/8/2011
* http://benalman.com/projects/jquery-misc-plugins/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
(function($) {
@tobsn
tobsn / jquery.loader.js
Created December 10, 2010 19:50
there are many problems when you act as third party that offers a javascript file and you want to use jquery - this solved all of my problems. the little function checks if jquery is already embedded, includes it and waits until its loaded if its missing
(function(j,q,u,e,r,y,R,o,x){try{o=jQuery;if(o&&(!R||(R&&o.fn.jquery==R))){x=true}}catch(er){}if(!x||(R&&o.fn.jquery!=R)){(q=j.createElement(q)).type='text/javascript';if(r){q.async=true}q.src='//ajax.googleapis.com/ajax/libs/jquery/'+(R||1)+'/jquery.min.js';u=j.getElementsByTagName(u)[0];q.onload=q.onreadystatechange=(function(){if(!e&&(!this.readyState||this.readyState=='loaded'||this.readyState=='complete')){e=true;x=jQuery;jQuery.noConflict(true)(function(){y(x)});q.onload=q.onreadystatechange=null;u.removeChild(q)}});u.appendChild(q)}else{y(o)}})(document,'script','head',false,false,(function($){$(function(){
/* code goes here */
console.log($.fn.jquery);
})}));
// readable:
(function (j, q, u, e, r, y, R, o, x ) {
// IE8 undefined crash fix
try {
@cowboy
cowboy / HEY-YOU.md
Last active May 16, 2024 13:31
jQuery Tiny Pub/Sub: A really, really, REALLY tiny pub/sub implementation for jQuery.
@rwaldron
rwaldron / idb.html
Created October 19, 2010 22:58
Working through the indexedDB API that was released in Chromium 8.0.552.5 dev
<!doctype html>
<html>
<head>
<title>IDB* API</title>
<script src="idb.js"></script>
</head>
<body>
<input type="button" id="button" value="message" />
</body>
</html>
@paulirish
paulirish / utmstrip.user.js
Last active April 11, 2024 07:53
userscript: Drop the UTM params from a URL when the page loads
// ==UserScript==
// @name UTM param stripper
// @author Paul Irish
// @namespace http://github.com/paulirish
// @version 1.2
// @description Drop the UTM params from a URL when the page loads.
// @extra Cuz you know they're all ugly n shit.
// @include http*://*
// ==/UserScript==
@cowboy
cowboy / firefox_multifix.sh
Created September 20, 2010 16:47
Firefox Multifix
#!/bin/bash
# Firefox Multifix
#
# Run this bash script from within the folder that contains your Firefox apps,
# and every Firefox app in the current folder will be modified to automatically
# launch using a version-named profile. For example, "Firefox 2.0.app" will use
# the "Firefox 2.0" profile, etc.
#
# Usage: