Skip to content

Instantly share code, notes, and snippets.

var listener = function ( fun ) {
return function ( e ) {
//thank you IE for the wonderful fish
e = e || window.event;
e.target = e.target || e.srcElement;
e.stopPropagation = e.stopPropagation || function () {
this.cancelBubble = true;
};
e.preventDefault = e.preventDefault || function () {
this.returnValue = false;
@Zirak
Zirak / gist:1490195
Created December 17, 2011 13:21
Basic DOM element manipulation
//creating an element is easy peasy
var divElem = document.createElement( 'div' );
//divElem is now a div element. it's not related to the any other element or
// node, it's free-range.
//to add it to the body element, for example:
document.body.appendChild( divElem );
//splendidsimo!
@Zirak
Zirak / gist:1510466
Created December 22, 2011 14:25
Introduction to javascript
//a simple introduction to javascript
//javascript comments are C-style: inline-comments begin with two forward
//slashes //, and end when on EOL (End Of Line)
/* multi-line comments are also available, spanning from forward-slash asterisk /*
until an asterisk forward-slash */
//variable declaration
@Zirak
Zirak / IO.js
Created December 24, 2011 22:47
SO chat hangman bot
var IO = {
//event handling
events : {},
preventDefault : false,
//register for an event
register : function ( name, fun, thisArg ) {
if ( !this.events[name] ) {
this.events[ name ] = [];
}
@Zirak
Zirak / game-min.js
Created January 14, 2012 17:32
Chain Reaction in under 1k
m=Math;R=m.random;P=m.pow;D=c.width=c.height=600;t=12;T=D-t;O=256;l=e=E=N=0;setInterval(function(){if(!E&&(e>=N||L&&e<N&&l--))e=L=0,A=[],A.length=5*++l,N=l*(l+1)/2;a.fillStyle=e<N?"#778":"#eef";a.fillRect(0,0,D,D);a.strokeText(e+"/"+N,9,9);for(i=A.length;i--;)with(A[i]||(A[i]={s:L,d:L,M:40*R()+20,x:L?L.clientX:R()*T+6,y:L?L.clientY:R()*T+6,v:R()*t-6,z:R()*t-6,C:"rgb("+[R()*O|0,R()*O|0,R()*O|0].join()+")",r:6,t:30/l|0}))!d&&A.some(function(b){return b.d&&P(r+b.r,2)>P(x-b.x,2)+P(y-b.y,2)&&(s=d=1,E++,e++)}),s?t?2==s?t--:++r>M&&(s=2):--r||(A.splice(i,1),E--):(x+=v,y+=z,x<r|x+r>D&&(v=-v),y<r|y+r>D&&(z=-z)),a.beginPath(),a.fillStyle=C,a.arc(x,y,r,0,7),a.fill()},50);c.onclick=function(b){L||(L=b,E++,A.push(0))};
@Zirak
Zirak / a.js
Created January 24, 2012 18:00
ol reverse attribute polyfill
//a polyfill for the ordered-list reversed attribute
// http://www.whatwg.org/specs/web-apps/current-work/multipage/grouping-content.html#the-ol-element
// http://www.whatwg.org/specs/web-apps/current-work/multipage/grouping-content.html#dom-li-value
//uses these awesomeness:
// Array.prototype.forEach
// Element.prototype.children
//if you want support for older browsers *cough*IE8-*cough*, then use the other
// file provided
(function () {
@Zirak
Zirak / gist:1677020
Created January 25, 2012 16:15
Visibility API
//http://www.w3.org/TR/2011/WD-page-visibility-20110602/
(function () {
"use strict";
//no need to shim if it's already there
if ( 'hidden' in document && 'onvisibilitychange' in document ) {
return;
}
//fail silently if the browser sux
@Zirak
Zirak / gist:1761880
Created February 7, 2012 20:52
A parser for DnD dice rolls
//infix operator-precedence parser
//also supports a d operator - a dice roll
var parsePrecedence = (function () {
//we don't care about whitespace. well, most whitespace
var whitespace = {
' ' : true,
'\t' : true
};
@Zirak
Zirak / gist:2000648
Created March 8, 2012 11:47
C's #include
//looks for comments at the beginning of a line looking like this:
//#build fileName
//and replaces them with fileName contents
//throws an error when fileName points to a file which doesn't exist
//first parameter is the file on which to operate,
//second parameter is a function which is called at the of operation with the
// new content
//no changes are done to the original file
@Zirak
Zirak / gist:2045479
Created March 15, 2012 17:30
QC scripty
//a script I hacked together in 10 minutes for easily reading Questionable Content:
// http://questionablecontent.net
//A and D keys, and their corresponding arrow keys, for movement between comic strips
//don't be alarmed if the image is a broken one, wait some more. if the script really failed
//to load a comic strip, it alerts a message
var s = document.getElementById( 'strip' ).cloneNode(),
l = document.createElement( 'p' ),
acts = [],