views:

643

answers:

3

for example if I have extjs ComboBox, and I have URL like http://localhost:8080?param=value1,value2,value3

how can I access this << param >> value in ExtJS, so that I can covert it into something like Ext.data.Array or ....

+1  A: 

I use a JavaScript file I found a while ago:

/* Client-side access to querystring name=value pairs
    Version 1.2.3
    22 Jun 2005
    Adam Vandenberg
*/

function QueryString(qs) { // optionally pass a querystring to parse
    this.params = new Object()
    this.get = QueryString_get

    if (qs == null)
        qs=location.search.substring(1,location.search.length)

    if (qs.length == 0) return

// Turn <plus> back to <space>
// See: http://www.w3.org/TR/REC-html40/interact/forms.html#h-17.13.4.1
    qs = qs.replace(/\+/g, ' ')
    var args = qs.split('&') // parse out name/value pairs separated via &

// split out each name=value pair
    for (var i = 0, len = args.length; i < len; i++) {
        var value;
        var pair = args[i].split('=')
        var name = unescape(pair[0])

        if (pair.length == 2)
            value = unescape(pair[1])
        else
            value = name

        this.params[name] = value
    }
}

function QueryString_get(key, default_) {
    // This silly looking line changes UNDEFINED to NULL
    if (default_ == null) default_ = null;

    var value=this.params[key]
    if (value==null) value=default_;

    return value
}

What you do then is:

var param = new QueryString().get("param");
var values = param.split(",");

values will then be your array.

Lloyd
+2  A: 

Use the framework to decode the query string:

var params = Ext.urlDecode(location.search.substring(1));
var array = [];
for (var name in params) {
    array.push([name, params[name]]);
}

Now array is suitable to be used as the data to an ArrayStore with fields name and value.

Jonathan Julian
+1  A: 

i have this (based on the code @Lloyd found above):

var the_query_string = new Object();
(function() {
 var qs = location.search.substr(1).replace(/\+/g, ' ').split('&');
 for (var i = 0; i < qs.length; i++) {
  qs[i] = qs[i].split('=');
  if (qs[i][0])
   the_query_string[qs[i][0]] = decodeURIComponent(qs[i][1]);
 }
})();

so i can access any querystring variable globally. e.g. when
location.search = '?search=hello+world&foo=bar%3Dbar'
then
the_query_string.search = 'hello world' and
the_query_string.foo = 'bar=bar'

Lucas
your solution is somewhat similar to the solution provided by Lloyd
Tumbleweed