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

URL parsing methods for String class Atom Feed 1

In Brief Methods to parse an url, add query parameters, and/or return the raw location or parameters portions of the url.... more
# 's
 1// Copyright 2008 Siafoo.net
2// BSD License
3
4String.extend({
5 toQueryParams: function(){
6 var params = {}
7 this.replace('?','').split('&').each(function(kv){
8 if (kv != ''){
9 kv = kv.split('=')
10 if (kv.length == 1) params[kv[0]] = true
11 else if (kv.length == 2) params[kv[0]] = kv[1]
12 }
13 })
14 return params
15 },
16
17 addQueryParams: function(params){
18 return '?' + Object.toQueryString($extend(this.toQueryParams(),params.toQueryParams()))
19 },
20
21 location: function(){
22 if (this.contains('?')) return this.split('?')[0]
23 else return this
24 },
25
26 params: function(){
27 if (this.contains('?')) return this.split('?')[1]
28 else return ''
29 }
30})

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.