getrequestargs in javascript
Posted by ark, ,
Here's how to parse the query string (from a method="GET" form) in javascript, returns an object/map

Doesn't handle multiple values (e.g. form?foo=yes&foo=no)

I'm pissed that "hello".replace("l", "w") == "hewlo"
Also pissed that decode("hello+world") != "hello world"


function getrequestargs(url) {
var args = new Object();
url = String(url);
var idx = url.indexOf("?");
if (idx >= 0) {
var s = url.substring(idx + 1);
var pairs = s.split("&");
for (var i = 0; i < pairs.length; i++) {
var idx = pairs[i].indexOf("=");
if (idx > 0) {
pairs[i] = pairs[i].replace(/\+/g, " ");
var key = decodeURI(pairs[i].substring(0, idx));
var value = decodeURI(pairs[i].substring(idx + 1));
args[key] = value;
}
}
}
return args;
}

var args = getrequestargs(location);

Comments

Posted Tuesday 7 August 2007 Share