-
Notifications
You must be signed in to change notification settings - Fork 0
/
jspara.js
93 lines (76 loc) · 2.25 KB
/
jspara.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
JsPara a simple javascript class for extracting GET parameters from a URL [add some other *BONUS* functions ;)]
@author: manujith pallewatte [[email protected]]
@code : this code is free to distribute, edit, reuse in any possible way.
*NOTE* still in BETA level so please report bugs, feature addition requests to above email
*/
function JsPara(url){
//decide the encoding
this.url = decodeURI(url);
//store protocol
this.protocol = url.protocol;
//store host
this.hostname = url.hostname;
//store port
this.port = url.port;
//store path
this.path = url.pathname
//first process query string
//get the parameter part
//var para_str = this.url.substr(this.url.indexOf("?")+1);
var para_str = decodeURIComponent(url.search.substr(1)).replace( /\+/g, ' ' );
var para_hash = decodeURIComponent(url.hash.substr(1)).replace( /\+/g, ' ' );
var matches = [];
//find for perfect type one i.e para = value
if(para_str != ""){
matches = para_str.match(/[A-z0-9_ .,\'\"\-]+=[A-z0-9_ .,\'\"\-]+/g);
}
if(para_hash != ""){
matches = matches.concat(para_hash.match(/[A-z0-9_ .,\'\"\-]+=[A-z0-9_ .,\'\"\-]+/g));
}
this.params = [];
this.param = [];
//assign those to instance variables
if(matches != null){
for(var i=0;i<matches.length;i++){
if(matches[i] != null){
var parts = matches[i].split("=");
this.params.push(parts[0]);
this.param[parts[0]] = parts[1];
}
}
}
//match for null parameters
if(para_str != ""){
matches = para_str.match(/&[A-z0-9. _,\'\"\-]+/g);
}
if(matches == null){
matches = [];
}
if(para_hash != ""){
matches = matches.concat(para_hash.match(/&[A-z0-9. _,\'\"\-]+/g));
}
//assing null value to those
if(matches != null){
for(var i=0;i<matches.length;i++){
if(matches[i] != null){
var parts = matches[i].split("&");
if(typeof this.param[parts[1]] == "undefined"){
this.params.push(parts[1]);
this.param[parts[1]] = null;
}
}
}
}
}
//function to build array from the query parameter string
JsPara.prototype.getAsArray = function(pname){
var ary_s = this.param[pname];
ary_s = ary_s.substr(1,ary_s.length-2);
var elems = ary_s.split(",");
var ary = [];
for(var i=0;i<elems.length;i++){
ary.push(elems[i]);
}
return ary;
}