Woah Nelly, nice overkill but I'm afraid it doesn't comply with the standards very well. The semicolon (;) is a valid GET parameter separator as well. Your functions plus that feature can be written with one regex each:
Code:
function UrlVariable(varname, isvalue) {
var m = location.href.substr(location.href.indexOf("?") + 1).match(new RegExp(isvalue ? "([^;&]+)=" + varname : varname + "=([^;&]+)"));
return m && m[1];
}
If we wanted to be really standards-compliant, though, we'd have to work with multiple values to the same variable too:
Code:
function UrlVariable(varname, isvalue) {
var m = location.href.substr(location.href.indexOf("?") + 1).match(new RegExp(isvalue ? "([^;&]+)=" + varname : varname + "=([^;&]+)", "g")),
r = [];
for(var i = 0, n = m.length; i < n; ++i)
r.push(isvalue ? m[i].substr(0, m[i].indexOf("=")) : m[i].substr(m[i].indexOf("=") + 1));
return r;
}
Either way, the code doesn't come close to looking like that mass of string manipulation you've got going on there.
There was a much more elegant implementation that mwinter wrote at one point, but I can't find it now.
Bookmarks