Hi Jörn,

I seem to be missing something (so much to learn!). Your note creates an 
anonymous function that assigns its result to jQuery.query, but where 
does the function get attached and how does it get invoked, like in 
Luke's example.

Thanks,
   -Steve

Jörn Zaefferer wrote:
> Luke Lutman schrieb:
>> 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)
>>   
> In that case, you should just assign the result to jQuery.query, eg:
> (function() {
> 
>       jQuery.query = 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;
>       });
> 
> })();
> 
> Considering that location.search does not change. I hope I'm not 
> confusing something.
> 
> -- Jörn
> 
> _______________________________________________
> jQuery mailing list
> discuss@jquery.com
> http://jquery.com/discuss/


_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to