Skip to content

Instantly share code, notes, and snippets.

@kara-ryli
kara-ryli / jshintrc-plus-grunt.js
Last active August 29, 2015 13:57
grunt-contrib-jshint requires *either* a jshintrc file or other options. I reject that dichotomy.
'use strict';
module.exports = function (grunt) {
var jshintrc = grunt.file.readJSON('.jshintrc');
// super simple shallow copy/merge
function merge(o1, o2) {
var retVal = {}, i;
for (i in o1) {
@kara-ryli
kara-ryli / v8-opt.js
Created February 6, 2015 23:02
A tip I picked up from Fluent 2014
// V8 initializes memory space for _x and _y
// in advance
function Klass1() {
this._x = null;
this._y = null;
}
var k1 = new Klass1()._x = 'hi';
function Klass2() {}
// dynamically defined property converts to a
@kara-ryli
kara-ryli / templatize.js
Created May 28, 2009 17:43
Changes the innerHTML of an element into a Template object.
Element.addMethods({
/**
* Browser caveats: IE6 does not add quotes to DOM attributes
* when it does not need to. Use `class="#{className} ie"` if
* you're going to replace attribute values with spaces. Also,
* most browsers load `<img src="#{src}" />` as a 404.
*/
templatize: (function(){
var FIND = /(?:#|%23)%7B([^%]+)%7D/g, REPLACE = "#{$1}",
ERROR = 'Could not templatize element ', EMPTY = "";
@kara-ryli
kara-ryli / Protototype Globals
Created July 27, 2009 19:23
JSLint requires you to define global methods. Use this list to make JSLinting your code easier.
$,$$,$A,$F,$H,$R,$w,Ajax,Class,Element,Enumberable,Event,Form,Hash,Insertion,PeriodicalExecuter,Position,Prototype,Template,TimedObserver,Try
@kara-ryli
kara-ryli / Geo.js
Created July 28, 2009 02:22
A primer for using the W3C geolocation API supported in Firefox 3.5 and MobileSafari
var Geo = navigator.geolocation, // {Object|null} a shortcut to the geolocation static class
options = { // {Object} options available to calls for position
enableHighAccuracy: false, // {Boolean} Whether or not to use more resources to get a more accurate position
maximumAge: 0, // {Number|Infinity} The maximum number of milliseconds since the last check.
timeout: null // {Number|null} The maximum number of milliseconds to wait for a fix
},
watchId; // {Number} an ID to a watch timeout
// handles an error finding the user's position
function onError(error) {
@kara-ryli
kara-ryli / SQLiteDB.js
Created July 28, 2009 21:15
A wrapper class for HTML5 client-side storage that makes the methods more easy to document.
/**
* SQLiteDB
*
* A wrapper class to make sane the HTML 5 client-side storage API.
* Makes your code a little bit more self-documenting.
*/
var SQLiteDB = (function () {
var db = function(args) {
if ( SQLiteDB.available ) { this.initialize.apply(this, args); }
else { throw new Error("database not available"); }
@kara-ryli
kara-ryli / slideshow.html
Created January 30, 2010 00:04
A Webkit-only version of Jonathon Snook's "Simplest jQuery Slideshow" that meets his requirements (concise, less than 20 lines) without requiring jQuery.
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Simplest jQuery Slideshow</title>
<style>
body { font-family:Arial, Helvetica, sans-serif; font-size:12px; }
.fadein { position:relative; height:332px; width:500px; }
.fadein img { position:absolute; left:0; top:0;}
.fadein.ready img { -webkit-transition: opacity 1s linear;}
</style>
@kara-ryli
kara-ryli / mouse-events.html
Created February 2, 2010 22:56
Logs the order of mouse events on an element
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
p { font: normal normal 10px/15px sans-serif; margin: 0;}
div { display: inline; float: left; height:300px;
overflow:auto; margin-right: 20px; width: 300px; }
#test { background: #333; color: #FFF; line-height: 300px;
@kara-ryli
kara-ryli / Dispatcher.as
Created February 23, 2010 02:22
How to dispatch Prototype custom events from Flash
package {
import flash.events.Event
import flash.events.IEventDispatcher;
import flash.external.ExternalInterface;
import CustomEvent;
public class Dispatcher {
private static const FIRE:String = 'Element.fire';
private static const PREFIX:String = 'flash:';
@kara-ryli
kara-ryli / Number.js
Created February 24, 2010 01:52
Number.protototype entry for wtfjs
(1) === 1; // true
Number.prototype.isOne = function () { return this === 1; }
(1).isOne(); // false!
Number.prototype.reallyIsOne = function () { return this - 1 === 0; }
(1).reallyIsOne(); // true