Chris W. Parker wrote:
> 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?
Hello, following my previous reply to this, I've had to implement this
myself. So here it is, attached.
This returns an object containing the properties:
- as specified in RFC3986 (URI Generic syntax)
- compatible with the window.location object
- the query string split into key=>value pairs
You can cut out what you don't need.
(I can't believe a JS mailing list bounces js attachments,
I try again with it pasted inline!)
jQuery.parseURI = function(uri) {
var m =
/^(([^:\/?#]+):)?(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/.exec(uri);
var q = m[7].split('&');
var p = {};
while (q.length > 0) {
var x = q.shift().split('=');
p[x[0]] = x[1];
}
return {
// Generic components (as specified in RFC3986)
scheme: m[2],
authority: m[4],
path: m[5],
query: m[7],
fragment: m[9],
// window.location compatible properties
hash: m[8],
host: m[4],
hostname: m[4].split(':')[0],
href: uri,
pathname: m[5],
port: m[4].split(':')[1] || '',
protocol: m[1],
search: m[6],
// The parsed query string:
params: p
};
};
I've also uploaded it here:
http://jollytoad.googlepages.com/uri.js
_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/