tomhoppe.com

Racing, Web Development, Photography, and Beer...Stuff that matters.

Archive for February, 2008

Add class name to div id

Monday, February 11th, 2008

Here is a script to add an extra class name to a div with an id regardless of what is there now.

function addClassName (id, classname) {
   var e;
   if (typeof(id) == 'string') {e = this.getElementById(id);}
   else {e = id;}

   if (!e || typeof e.className != 'string') {
      throw "Cannot add class to element " + id;
   }

   /* Check if the class is already there */
   if (!e.className.match(new RegExp('\\b' + classname + '\\b'))) {
      e.className += ' ' + classname;
   }
}

Grab filename from window location

Monday, February 11th, 2008

Here is a snippet I use to grab the filename from the full window location of page.

var filename = location.pathname.substr(location.pathname.lastIndexOf("/")+1,location.pathname.length);