Same comment as before -- this function is a general purpose URL manipulation routine and belongs in a separate class/module. Before we start copying and pasting code from SO (stackoverflow), let's see if there are any existing JS libraries that can do that for us. 

I'm always wary of copying & pasting code like that because it can easily lead to [cargo cult programming](http://en.wikipedia.org/wiki/Cargo_cult_programming). We also need to be careful of licensing issues when copying code versus reusing an existing library released under a standard license. Stackoverflow's policy is [cc-wiki, attribute required](http://blog.stackoverflow.com/2009/06/attribution-required/), which it looks like we're following, so that's good. 

The bottom line is that before copying code from SO, we should have a good reason to be using that code versus an existing library, and if we do use that code, try to modularize it if possible (i.e. put it in a separate module/class).