Erik's blog

Code, notes, recipes, general musings

caja-safe roots javascript utils

leave a comment »

var foreach = function(collection, callback){//tested: ff 3 mac
if(collection.length){//array or node list
for(var i = 0; i < collection.length; i++){ callback(i, collection[i]); } }else if(collection.hasOwnProperty){ for(var key in collection){ if(collection.hasOwnProperty(key)){ callback(key, collection[key]); } } }else{ throw('each() error: collection (' + collection + ') is neither an array nor an object'); } }, getElementsByClassName = function(elements, className){ var nodes = []; foreach(elements, function(i,element){ if(className === element.className){ nodes.push(element); } }); return nodes; }, hasClass = function(node, className){//tested: ff 2/3 win/mac, ie 6/7 win return new RegExp('[\b]*' + className + '[\b]*').test(node.className); }, [/sourcecode]

Advertisements

Written by Erik

February 22, 2009 at 1:12 pm

Posted in code

Tagged with

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: