Skip to content

Instantly share code, notes, and snippets.

@BrynM
Created March 14, 2012 08:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrynM/2035091 to your computer and use it in GitHub Desktop.
Save BrynM/2035091 to your computer and use it in GitHub Desktop.
JS Trickery
// editable web page
(function(){
document.body.contentEditable='true';
document.designMode='on';
})();
// merge arrays
(function(){
var a = [1,2,3];
b = [6,5,4];
Array.prototype.push.apply(a, b);
})();
// randomize an array
(function(){
[1,2,3,4,5,6,7,8,9].sort(
function() {
Math.random() - 0.5
}
);
})();
// call stack
(function(){
try {
throw Error();
} catch( eR ) {
return eR.stack;
}
})();
// watch a member (js 1.8/Gecko)
(function(){
var x = {
'thing' : 'value'
};
if ( typeof(x.watch) == 'function' ) {
x.watch( 'thing', function ( ev ) {
console.log( 'watched thing', ev );
} );
}
})();
// regex pointers
(function(){
('this is bar').match( /( ba)r$/ );
RegExp.$1;
})();
// crazy img tags
(function(){
var R=0,
x1 = .1,
y1 = .05,
x2 = .25,
y2 = .24,
x3 = 1.6,
y3 = .24,
x4 = 300,
y4 = 200,
x5 = 300,
y5 = 200,
DI = document.getElementsByTagName( 'img' ),
DIL = DI.length,
DIS = null;
function CHAOS () {
for ( i=0; i-DIL; i++ ) {
document.body.appendChild( DI[i] );
DIS = DI[i].style;
DIS.position = 'absolute';
DIS.left = ( Math.sin( R * x1 + i * x2 + x3 ) *x4 + x5 ) + 'px';
DIS.top = ( Math.cos( R * y1 + i * y2 + y3 ) * y4 + y5) + 'px';
}
R++;
}
setInterval( 'CHAOS()', 5 );
})();
// ascii to HTML decimal
(function(){
function assHtm (){
var cO = '',
cT = [].splice.call( arguments, 0 ).join( '' );
for ( cI = 0; cI < cT.length; cI++ ) {
cO += '&#' + cT.charCodeAt( cI ) + ';';
}
return cO;
}
assHtm( 'things' );
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment