Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

malte (1708)

  (email not shown publicly)

Working on Joose JavaScript meta system (Blog) [] and blok [], a web based application for collaborative ui prototyping.

Journal of malte (1708)

Tuesday April 26, 2005
03:14 AM

CGI Module for JavaScript

[ #24374 ]
It only works for GET, but still a small step toward what should have been in JavaScript 1.0

function CGI() {

    var query =,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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • Wow. It's like it's 1997 all over again.

    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.
    • 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.

    • Why do you think that Matt Wright created the "foreach my $pair (@pairs)" snippet? I wrote it; it appeared in my form-mail program, which was distributed in late 1993 or early 1994.

      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 ( and that parsed parameters for you, why would you ever do it yourself in production code?

      I still have the e-mail f
      • Sorry. You're right. I knew that Matt's program was based on your code. We've discussed it before in email (when I found that UK ISP Demon were still distributing your original program).

        At least Matt now mentions [] 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 [].
  • 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.

  • Use; 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 of this[pair[0]] = pair[1]. Remember to check for non-existent elements and assign them a new Array if needed, since JS does not have autovivification.