Kirk’s PRNG

"Anyone who considers arithmetical methods of producing random digits is, of course, in a state of sin." --John von Neumann

Javascript Function Overwriting

Browser incompatibilities are going to be with us for the foreseeable future. This can lead to lots of ugly and inefficient browser detection branches in javascript functions.

 
function foo(bar) 
{ 
  if(navigator.userAgent.toLowerCase().indexOf('msie') > -1) {
    //do Internet Explorer Stuff 
  } 
  else { 
    //do Firefox Stuff 
  } 
} 

Instead we can take advantage of scoping and first-class functions to rewrite the function during the first execution, allowing each subsequent execution to skip the branching.

 
function foo(bar) 
{ 
  if(navigator.userAgent.toLowerCase().indexOf('msie') > -1) { //if 
Internet Explorer 
    //rewrite foo to only execute the internet explorer code 
    foo = function (bar) { 
      //do Internet Explorer Stuff 
    }; 
  } 
  else { 
    //rewrite foo to only execute the firefox code 
    foo = function(bar) { 
      //do Firefox Stuff 
    }; 
  } 
 
  //after rewriting the function, call it so the original caller 
  //receives what they need 
  return foo(bar); 
} 

Filed under  //   javascript  
Posted January 20, 2009
// 0 Comments

Javascript URL Strings

Here's a simple trick I found while looking at some JS library source code today.

If you need to specify a url such as "http://domain.tld/path" in javascript code, some parsing engines may error out with an unterminated string exception after they hit the // and switch into comment mode.

To prevent this, split the string into two parts as shown below.

 var url = "http:/" + "/domain.tld/path"; 

Filed under  //   javascript  
Posted January 19, 2009
// 0 Comments

Web Design Resources List from Elementiks

Fantastic list of tools, libraries, content and references all useful for web design and coding. Javascript section is weighted toward jQuery, but everything else is fairly universal.
web resources I use

My favorite things so far are a pair of bookmarklets:
http://www.sprymedia.co.uk/article/Design - overlay, grid, and measuring tools.
http://westciv.com/xray/ - mini firebug inspector. Click any element and see style details.

Filed under  //   css   design   html   javascript   utilities   web  
Posted January 19, 2009
// 0 Comments

Aptana ScriptDoc Zenburn Color Scheme

Follow up to: Zenburn For Aptana

Here's another file for import into Aptana's ScriptDoc Editor Colors panel to make documentation look right.

Also, under Preferences->General->Editors->Text Editors I set Line Number Foreground to RGB:133,172,141

The end result should match the attached screenshot.

Click here to download:
Aptana_ScriptDoc_ZenBurn.col (1 KB)

Filed under  //   aptana   color scheme   ide   javascript   scriptdoc   zenburn  
Posted January 16, 2009
// 2 Comments

ActiveRecord.js Released as Beta: ORM in Javascript

ActiveRecord.js Released as Beta

Client-side persistent apps now have a library to support the ActiveRecord ORM pattern against Google Gears, Adobe Air, and future browser storage mechanisms (HTML5).

The library ships in a single file, has no external dependencies, and runs client side in all major browsers with or without access to a SQL database

Filed under  //   ActiveRecord   javascript   ORM  
Posted January 14, 2009
// 0 Comments

Static Typing

I'm a recent convert to the glories of Javascript. Here's a great quote from Jeff McFadden

Static typing is like introducing yourself and having to explain who your grandparents are before you can talk about the weather.

Filed under  //   javascript   quotes   static typing  
Posted January 14, 2009
// 0 Comments

Zenburn for Aptana Javascript

Click here to download:
Aptana_JS_ZenBurn.col (1 KB)

Visual Studio won't support Javascript snippets until 2010, so I'm switching to Aptana Studio.
Of course an editor isn't an editor until it supports your color scheme, so I now submit to the world Zenburn for Aptana.

If you use ScriptDoc, be sure to get this color scheme file as well: /aptana-scriptdoc-zenburn-color

Aptana ScriptDoc ZenBurn Color Scheme

Filed under  //   aptana   color scheme   ide   javascript   zenburn  
Posted January 14, 2009
// 0 Comments