tomhoppe.com

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

Tuesday, July 17, 2007

Assign the child's href to the parent's onclick

This is a cool little snippet for making realistic CSS buttons. It will do an onclick location on the element to the href of its first a element.

Makes it nice when you have a "fake button" done through CSS and you want the whole thing to be clickable.

What I usually try to do is CSS the A tag display:block and assign it a width, height and padding, but when you can't do that, this little snippet is a life saver

onclick="if (this.getElementsByTagName('a')[0])
location=this.getElementsByTagName('a')[0].href;"

Labels: , ,

Querystring parsing

Here is a snippet that will pull out and parse out the query string using JS. It separates the query string into two arrays. One with the variable names and the other with the values. You can then loop through the arrays to get your information. This script is useful if you have lots of variables in your querystring, or are not sure what is going to be coming in. I also have another snippet which just grabs the value of one variable instead.

function QueryString(key){
  var value = null;
  for (var i=0;i<QueryString.keys.length;i++) {
    if (QueryString.keys[i]==key)  {
      value = QueryString.values[i];
      break;
    }
  }
  return value;
}

QueryString.keys = new Array();
QueryString.values = new Array();

function QueryString_Parse(){
  var query = window.location.search.substring(1);
  var pairs = query.split("&");
 
  for (var i=0;i<pairs.length;i++) {
    var pos = pairs[i].indexOf('=');
    if (pos >= 0)  {
      var argname = pairs[i].substring(0,pos);
      var value = pairs[i].substring(pos+1);
      QueryString.keys[QueryString.keys.length] = argname;
      QueryString.values[QueryString.values.length] = value;  
    }
  }
}

QueryString_Parse();

Labels: ,

Find something in a string

Here is a quick snippet that sets a variable to the location of a certain string inside another string. The example I'm using is searching for "asdf" in the URL. You can use this to tell if you are on a certain page.

var theUrl= location.href;
var is_asdf = (theUrl.indexOf("asdf") != -1);

Labels: ,

Font sizer for a content well

We are using this font sizer where I work. It change the font size on all tags inside of div "textArea" that do not have a specifc font size defined. That way, your regular p tags and headers will font size, but if you must keep something the same, you can override. All it does it set a class on the content container div, defining the font size within. Its too long to include as a snippet, so you can get it here.

Labels: ,