Johannes Theile schrieb:
> Hi,
> 
> 
> Klaus Hartl wrote:
>> Johannes, can't you just use a query parameter? Your description sounds 
>> exactly like this is what you need.
> 
> sorry, but currently it is unclear to me, what you mean by a query
> parameter. A parameter which is attached to the URL?
> 
> Maybe you could give me a hint and how to get the value of such a query
> parameter. I'm still really new to jQuery.
> 
> 
> Kind regards,
> Johannes


I meant a parameter like this: index.php?foo=bar&something=else

Here's a little helper to extract such parameters:

jQuery.query = function(s) {
     var r = {};
     if (s) {
         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 numbers
             if (/^[0-9.]+$/.test(val)) val = parseFloat(val);
             // convert booleans
             if (val == 'true') val = true;
             if (val == 'false') val = false;
             // ignore empty values
             if (typeof val == 'number' || typeof val == 'boolean' || 
val.length > 0) r[key] = val;
         });
     }
     return r;
};

Usage:

var params = $.query( $('a').attr('href') ); // { foo: 'bar', something: 
'else'}
alert(params.foo); // 'bar'


-- Klaus



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

Reply via email to