I was thinking of something a little fancier ;-)

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;
};

Then you could use it like so:

http://www.bork.com?foo=helloworld&bar=0.333&;

$.query()['foo']; // helloworld (string)
$.query()['bar']; // 0.333 (number)



Luke


Sam Collett wrote:
> On 20/10/06, Luke Lutman <[EMAIL PROTECTED]> wrote:
>> Is there a jQuery method or plugin for parsing the browser's query string 
>> into an object?
>>
>> I looked through the api and did a bit of searching around, but no luck ...
>>
>> Thanks,
>> Luke
>>
>> --
>> zinc Roe Design
>> www.zincroe.com
>> (647) 477-6016
> 
> 
> You don't need jQuery for that. You can get the query string by using
> 'location.search'.
> 
> if(location.search.length !=0)
> {
>     // create an array
>     var qs = location.search.substr(1).split("&");
>     // get first parameter value
>     alert(qs[0].split("=")[1]);
> }
> 
> _______________________________________________
> jQuery mailing list
> [email protected]
> http://jquery.com/discuss/


-- 
zinc Roe Design
www.zincroe.com
(647) 477-6016

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to