Search code examples
javascriptprototypejs

Accessing CGI values with Prototype


Anyone know the quickest way to grab the value of a CGI variable in the current URL using Prototype? So if I get redirected to a page with the following URL:

http://mysite.com/content?x=foobar

I am interested in retrieving the value of "x" (should be "foobar") in a function called on page load like this:

Event.observe(window, "load", my_fxn ());

Thanks


Solution

  • You may want to look at the parseQuery method. This should do all the splitting you'd expect on a standard querystring such as document.location.search

    http://api.prototypejs.org/language/string.html#parsequery-instance_method

    For example:

    document.location.search.parseQuery()["x"];

    Will be undefined if it's not present, and should be the value otherwise.