I guess there is a library somewhere to do this, but I couldn’t find it fast enough so I spit this contraption out. It’s a function that grabs the value from a URI/URL query string. Yes, writing those nasty REGEXs about drove me nuts! 😉 Maybe I can save at least one soul out there some trouble. If you see an enhancement option or have ideas, please comment and let me know!
[sourcecode language=”javascript”]
function GiveMeTheQueryStringParameterValue(parameterName) {
parameterName = parameterName.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regex = new RegExp("[\\?&]" + parameterName + "=([^&#]*)");
var results = regex.exec(window.location.href);
if (results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
[/sourcecode]
http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript
or
https://github.com/allmarkedup/jQuery-URL-Parser
I like the version at the bottom of the Stack Overflow page. The problem with these regex solutions (of which I have written many) is that they’re heavy. If they have to execute repeatedly, they add up to a lot of overhead.
mp
Oh yeah, instantiating REGEX over and over can easily kill performance. I was just using this as a one shot, but will take a look at those other solutions. thanks!