Skip to content

Instantly share code, notes, and snippets.

View isaacs's full-sized avatar
🔮
only way out is through

isaacs isaacs

🔮
only way out is through
View GitHub Profile
/**
* Turn an object into a query-string.
* @param obj {Object} The thing to turn into a query string.
* @return {String} The query-string serialization.
* Note: "lang" refers to YAHOO.lang
**/
var objToQueryString = function (obj, name) {
if (lang.isNull(obj) || lang.isUndefined(obj)) {
return name ? encodeURIComponent(name) + '=' : '';
}
// How awful is this approach? Really awful? Not awful?
// Cuz it seems not awful, and actually really convenient.
// Note how MyComponent has a single instance of YUI(),
// but without having to wrap the whole thing in the YUI call.
// This means that MyComponent can be extended later on,
// and still use the same single instance of YUI.
MyComponent = (function () {
var myYUI = null
var load = function (fn) {

Use tabs for indentation.

This is not up for debate. Please configure your editor to use tabs instead of spaces. Tabs let everyone use the level of indentation that they prefer.

PascalClass for classes and singletons, camelCase for variables, functions, and members, css-case for HTML classes/IDs and file/folder names.

Example:

MyClass = function () {...};
var myObj = new MyClass();
<?php
$str = '
<a href="?class=blah">foo</a>
<a href=""><em>foo</em></a>
<a href="bar">Foo</a>
<a href="bar" style="blah" class="foo">Foo</a>
<a href="bar" class="foo">Foo</a>
<a class="foo" href="bar">Foo</a>
<a class="foo">Foo</a>
YUI('lang', function (Y) {
YAP.toQueryString = function (obj, name) {
if (Y.Lang.isNull(obj) || Y.Lang.isUndefined(obj)) {
return name ? encodeURIComponent(name) + '=' : '';
}
if (Y.Lang.isBoolean(obj)) obj = obj ? 1 : 0;
if (Y.Lang.isNumber(obj) || Y.Lang.isString(obj)) {
return encodeURIComponent(name) + '=' + encodeURIComponent(obj);
}
if (Y.Lang.isArray(obj)) {
YUI('lang', function (Y) {
YAP.toQueryString = function (obj, name) {
switch (Y.Lang.type(obj)) {
case 'undefined':
case 'null':
return name ? encodeURIComponent(name) + '=' : '';
case 'boolean': obj = +obj; // intentional fallthrough
case 'number':
case 'string':
var parseQueryString = (function (overlap) {
return function (qs) {
var kv = qs.split('&');
var obj = {};
for (var i = 0, l = kv.length; i < l; i ++) {
kv[i] = kv[i].split('=');
var key = decodeURIComponent(kv[i].shift());
var val = decodeURIComponent(kv[i].join('='));
var keyparts = key.match(/^([^\[]+)(.*)$/);
if (!keyparts) continue;
We couldn’t find that file to show.
// Create a new bookmark on your toolbar in Firefox,
// and paste this thing into the "location" field.
// Give it a name, and click it whenever you want to
// see what the password fields on forms says.
// I created this because sometimes I'm not sure if
// I typed it right, and want to check without
// deleting and typing it over.
javascript:Array.prototype.slice.call(document.body.getElementsByTagName('input'),0).filter(function(n){return%20n&&n.type&&(n.type.toLowerCase()==='password'||n._oldType==='password');}).forEach(function(n){if(n.type.toLowerCase()==='password'){n.type='text';n._oldType='password';}else{n.type=n._oldType;}});
@isaacs
isaacs / gist:157681
Created July 28, 2009 21:30
Javascript loading example
<!DOCTYPE html>
<html>
<head>
<script>function L(B,D){var A=document.createElement("script"),C=document.documentElement.firstChild;A.type="text/javascript";if(A.readyState){A.onreadystatechange=function(){if(A.readyState=="loaded"||A.readyState=="complete"){A.onreadystatechange=null;D()}}}else{A.onload=function(){D()}}A.src=B;C.insertBefore(A,C.firstChild)};
// async loading of javascript files, starting asap.
L("http://example.com/example.js",function () {
doSomething();
});
L("http://example.com/foo.js", function () {