tomhoppe.com

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

Archive for the ‘Javascript’ Category

Cross browser, self closing popup window

Friday, March 7th, 2008

If you want to a cross browser way to have your popup self close

onsomething="window.opener=self;window.close();"

This works in most browsers, but doesn’t work in FF 2.0 unless the window was in fact popped up. The window.opener=self fools most browsers, but FF is smarter :-)

I know its bad usability, but the context I used it in, was that a window was popping up a poll. On submitting that poll, the poll popup would close itself and the results on the opener were updated. The code for that is below. I put that code on the submit button onclick. I had to name the opener window in order for it to work. This way if there is an opener, it will try to target “jjCalendar” as the forms target. If that name doesn’t exist, it will just open a new window.

onclick="if (window.opener){form.target='jjCalendar';}"

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);

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

Monday, July 16th, 2007

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;"

Querystring parsing

Monday, July 16th, 2007

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();

Find something in a string

Monday, July 16th, 2007

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);

Font sizer for a content well

Monday, July 16th, 2007

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.

  • Recent Posts

  • Categories

  • Archives