Explain yourself with reStructured Text, embedded code, graphs, charts, and LaTeX–style math.
Join Siafoo Now
or
Learn More
URL parsing methods for String class
Updated over 2 years ago (03 Mar 2008 at 06:10 PM)
recent activity
| In Brief | Methods to parse an url, add query parameters, and/or return the raw location or parameters portions of the url.... more |
| Language | JavaScript |
# '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.
Add a Comment