Chris W. Parker schrieb:
> On Tuesday, October 31, 2006 10:46 AM Luke Lutman <> said:
>
>> Have a look at this recent thread :-)
>>
> http://www.nabble.com/method-plugin-for-getting-query-string-vars--tf248
> 1232.html#a6919130
>
> I read through this and tried to implement your first suggestion but I
> notice that everything takes location.search and parses that. I want to
> use it in the following way:
>
> theHref = $(this).attr("href").query();
>
> Your function is:
>
> jQuery.query = function() {
> var r = {};
> var q = location.search;
> q = q.replace(/^\?/,''); // remove the leading ?
> q = q.replace(/\&$/,''); // remove the trailing &
> jQuery.each(q.split('&'), function(){
> var key = this.split('=')[0];
> var val = this.split('=')[1];
> // convert floats
> if(/^[0-9.]+$/.test(val))
> val = parseFloat(val);
> // ingnore empty values
> if(val)
> r[key] = val;
> });
> return r;
> };
>
> I'm not sure how to modify that function to do what I want (considering
> my current lack of JS/jQuery syntax). Would you mind showing me what to
> do?
>
>
> Thank you,
> Chris.
Hi Chris,
change it to that:
jQuery.query = function(s) {
var r = {};
var q = s.substring(s.indexOf('?') + 1); // remove everything up to
the ?
q = q.replace(/\&$/, ''); // remove the trailing &
jQuery.each(q.split('&'), function() {
var splitted = this.split('=');
var key = splitted[0];
var val = splitted[1];
// convert floats
if (/^[0-9.]+$/.test(val)) val = parseFloat(val);
// ignore empty values
if (typeof val == 'number' || val.length > 0) r[key] = val;
});
return r;
};
Usage:
var query = $.query( $(this).attr("href") );
or
var query = $.query( location.search );
var certainParam = query.nameOfParam;
I also fixed that if a parameter is 0 (like number=0) it is converted to
a float and then evaluates to false in the next line, so that it won't
be put into the params hash map.
I think converting booleans makes sense as well, what do you think?
Heres a demo:
http://stilbuero.de/demo/jquery/query.html?bar=foor&number=0
-- Klaus
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/