License New BSD license
Lines 29
Average
3.0
Rated
1
Times
5
4
3
2
1
0
Keywords
native class extension (3) parsing (1) string (7) url (3)
Included in this Library
Permissions
Owner: David
Group Owner: iCapsid
Viewable by Everyone
Editable by All Siafoo Users

URL parsing methods for String class Atom Feed

In Brief Methods to parse an url, add query parameters, and/or return the raw location or parameters portions of the url.... more
# 's
// Copyright 2008 Siafoo.net
// BSD License

String.extend({
toQueryParams: function(){
var params = {}
this.replace('?','').split('&').each(function(kv){
if (kv != ''){
kv = kv.split('=')
if (kv.length == 1) params[kv[0]] = true
else if (kv.length == 2) params[kv[0]] = kv[1]
}
})
return params
},

addQueryParams: function(params){
return '?' + Object.toQueryString($extend(this.toQueryParams(),params.toQueryParams()))
},

location: function(){
if (this.contains('?')) return this.split('?')[0]
else return this
},

params: function(){
if (this.contains('?')) return this.split('?')[1]
else return ''
}
})

Methods to parse an url, add query parameters, and/or return the raw location or parameters portions of the url.

Run with 'url'.method()

Requires mootools.