function CGI() {
var query = location.search.substr(1,256);
var pairs = query.split("&");
for(var i in pairs) {
var pair = pairs[i].split("=");
this[pair[0]] = pair[1]
}
CGI.prototype.param = function (name) {
var value = this[name]
if(value == null) {
return ""
}
return value
}
}
There are obviously bugs in this code (encoding) wise but it works for most purposes.
Matt Wright Lives! (Score:2)
You might like to read some of the criticism of Matt's CGI decoder that you'll find all over the web. And then incorporate fixes for those problems in your code.
Two obvious problems that spring to mind:
* No support for the ';' parameter separator which is now recommended over '&'.
* No support for multiple parameters with the same name.
Re:Matt Wright Lives! (Score:2)
I know. Thats why I said there are bugs in there. However, if you know you will only get & separators and all you want to transport via the URL is some integers or booleans values, than it works pretty well.
As opposed to Matt's decoder there is no security hole in that code.
All the bugs you mentioned are easily fixed. My point was, that having a thing like the CGI object in JavaScript is nice :) That's all.
Re:Matt Wright Lives! (Score:1)
Needless to say, this was when HTML forms were still brand new, and having a form that sent e-mail was considered novel. Once forms and CGI were more established, and once there were libraries (cgi-lib.pl and CGI.pm) that parsed parameters for you, why would you ever do it yourself in production code?
I still have the e-mail f
Re:Matt Wright Lives! (Score:2)
At least Matt now mentions [scriptarchive.com] the nms versions of these programs on his site. He even distributes slightly altered versions of them, tho' the links are hard to find and he refuses to link directly to our site [sf.net].
You know... (Score:1)
I was always of the mind that Javascript should have been extended further (db libraries, cgi, etc.).
When I was first learning it I was thinking it was a nice first language but it needed just a bit more.
Re: (Score:1)
Use
location.search.substr(1);instead of throwing away anything beyond the 256th character.Split on
/[;&]/, not"&".Call
decodeURIComponent()on your split strings.Use
this[pair[0]].push(pair[1])instead ofthis[pair[0]] = pair[1]. Remember to check for non-existent elements and assign them anew Arrayif needed, since JS does not have autovivification.