Thursday, June 12, 2008

Firefox add-ons

There are 3 Firefox add-ons I use daily. Together, they save me a lot of time during web development. I don't know how I ever lived without them.
  1. Firesizer
    • Lets you change your browser size to 800x600, 1024x768, etc.
    • Lets you create your own custom sizes
    • Always shows you the current dimensions of your browser
  2. IE Tab
    • Lets you view pages using an IE browser right inside Firefox.
    • This not only lets me stay in Firefox all day, but also lets me instantly switch browsers to see how a page looks in one or the other.
    • You can even run active-x controls when viewing in IE mode.
  3. Firebug
    • This one should just be called 'awesome'.
    • Lets you debug code, add, modify, or delete page elements while you're looking at the page
    • Lets you hover over any part of the page and see the elements and style hierarchy
    • I'll say it again: This one should just be called 'awesome'.
Firebug has been the most useful, but the other two are still awesome!

Tuesday, June 10, 2008

JavaScript endsWith() and trim()

Today I needed a new JavaScript string function to see if the end of a string matched something. In ASP.NET, when controls within other controls get written as HTML, the id's get prepended with a bunch of junk from the container controls. So, for your JavaScript functions to find a particular control, it's useful to use document.getElementsByTagName('input') for example to get a list of input tags, then iterate the list testing the end of the id attribute to find a particular control. Anyway, I used 'prototype' to add an endsWith(x) function to strings. Here it is. I hope you find it useful.
String.prototype.endsWith = function(txt,ignoreCase)
{
  var rgx;

  if(ignoreCase)
  {
    rgx = new RegExp(txt+"$","i");
  }
  else
  {
    rgx = new RegExp(txt+"$");
  }

  return this.match(rgx)!=null; 
}

For example: 'hello'.endsWith('LO',true) returns true. Notice I passed in a true value for the ignoreCase parameter. Or with a variable: var x = 'goodbye'; then x.endsWith('bye') returns true. While I'm here, here's a cool one liner that will do a trim() on both sides of a string.
String.prototype.trim = function()
{
  return this.replace(/(^\s*|\s*$)/g,'');
}
For example: alert('.' + '    blah    '.trim() + '.'); shows ".blah."

Wednesday, June 4, 2008

Response.Redirect ResolveUrl fails

I had a Response.Redirect fail today. The url was something like this: "~/blah/blah.aspx?id=5&time=3:30". Response.Redirect, when it sees the "~" character, does a ResolveUrl() call under the hood. That's really what was failing. It wouldn't resolve my url, so I kept removing parameters until it worked. It was choking on the ":" character. The solution was to do a myUrl.Replace(":","%3A"). I don't know why it can't handle the ":" and wonder if there are other characters it has a problem with.