Skip to content

Instantly share code, notes, and snippets.

View simondell's full-sized avatar

Simon Dell simondell

View GitHub Profile
@simondell
simondell / bug.js
Created October 26, 2012 14:18
A function for displaying debug messages in environments where console.log support is feeble (like pre-iOS6 Mobile Safari)
function debug(msg) {
var container = document.getElementsByTagName('body')[0],
bug = document.createElement('div'),
bugStyle = bug.style,
bugSplat;
console.log('msg');
bugStyle.position = 'absolute';
bugStyle.top = '50%';
@simondell
simondell / render.js
Created October 26, 2012 14:24
Tinkering with dynamically creating HTML from JS/JSON
/* ====================
Render
String builders for
- elements
- their attributes
======================*/
Render = function () {
/*
@simondell
simondell / match-button.txt
Created November 5, 2012 11:03
Find HTML buttons regex
<button([ a-zA-Z0-9_\-="]+)value="([ a-zA-Z0-9_\-]+)">(.*?)</button>
@simondell
simondell / index.html
Created December 4, 2012 15:07
A CodePen by Simon Dell. Android adds invisible target area - In a recent project, it seemed as though Android was adding invisible active areas around a submit button on a form. This was problematic as there was a link with a JS event handler pretty clos
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.5">
<title>submit button touch area test</title>
</head>
<body>
@simondell
simondell / isSimilar.js
Created March 4, 2013 10:03
A shallow object similarity checker. Not revolutionary, or even that good..
function isSimilar( o1, o2 ) {
var similar = true,
prop = null;
for( prop in o1 ) {
if ( o1.hasOwnProperty( prop ) ) {
similar &= ( o2.hasOwnProperty( prop ) && o1[prop] === o2[prop] );
} else {
return false;
}
}
@simondell
simondell / profile.js
Last active December 17, 2015 04:59
Profile JS operations easily! This function tells you the median number of times an operation will run per second, based on five separate counts. Pass it a name for the test and an operation to perform. Based on an idea in Raphaele Cecco's "Supercharged HTML Graphics", which in turn was inspired by code by John Resig.
/*
* @param: name - String, a label for the test - usually the function's name
* @param: operation - Function, the code to test
*/
var profile = function ( name, operation ) {
var iterate = function ( operation ) {
var start = new Date().getTime();
var elapsed = 0;
for( var itrs = 0; elapsed < 1000; itrs++ ) {
@simondell
simondell / _font-size.scss
Last active December 17, 2015 13:49
SASS Mixin for writing out equivalent font-sizes in px and rem units. Copied from somewhere. Probably Chris Coyler's CSS-Tricks.
// Calculate px and rem font-sizes
// - assumes html { font-size: 10px; }
@mixin font-size( $size ) {
$pixels: #{$size}px;
$rems: #{$size/10}rem;
font-size: $pixels;
font-size: $rems;
}
@simondell
simondell / regex_bg_image.js
Last active December 23, 2015 16:39
Regex to get parts of an image URL out of the string returned from elem.style.backgroundImage.
// finds an image path in the string returned from elem.style.backgroundImage
var = rgx = /(?:url\(['"]?)(\w[a-z-.\/]+)(?:['"]?\))/g;
@simondell
simondell / easy_to_read_module.js
Created October 18, 2013 09:37
An example "class", or a "type", or a "module". The example was created to highlight "private static functions", and their placement in the file. General JS community dialog favours highlighting the effect of variable and function "hoisting" by writing such object definitions at the top of their scope. For larger objects intended as classes or m…
var MyStructure = (function () {
return {
str: 'rush',
go: function () {
doThisFirst();
doThisNext( this );
return this;
}
}
var arg_ary = [ 1, 2, 3 ];
var arg_obj = { foo: 'bar' };
function reset( param ) {
param[0] = undefined;
console.log( "in reset", param );
}
console.log( arg_ary ); // [ 1, 2, 3 ]
reset( arg_ary ); // [ undefined, 2, 3 ]