-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathjquery.querystring.js
123 lines (105 loc) · 3.27 KB
/
jquery.querystring.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
(function($) {
// Naive method of yanking the querystring portion from a string (just splits on the first '?', if present).
function extractQuery(string) {
if(string.indexOf('?') >= 0) {
return string.split('?')[1];
} else if(string.indexOf('=') >= 0) {
return string;
} else {
return '';
}
};
// Returns the JavaScript value of a querystring parameter.
// Decodes the string & coerces it to the appropriate JavaScript type.
// Examples:
// 'Coffee%20and%20milk' => 'Coffee and milk'
// 'true' => true
// '21' => 21
function parseValue(value) {
value = decodeURIComponent(value);
try {
return JSON.parse(value);
} catch(e) {
return value;
}
}
// Takes a URL (or fragment) and parses the querystring portion into an object.
// Returns an empty object if there is no querystring.
function parse(url) {
var params = {},
query = extractQuery(url);
if(!query) {
return params;
}
$.each(query.split('&'), function(idx, pair) {
var key, value, oldValue;
pair = pair.split('=');
key = pair[0].replace('[]', ''); // FIXME
value = parseValue(pair[1] || '');
if (params.hasOwnProperty(key)) {
if (!params[key].push) {
oldValue = params[key];
params[key] = [oldValue];
}
params[key].push(value);
} else {
params[key] = value;
}
});
return params;
};
// Takes an object and converts it to a URL fragment suitable for use as a querystring.
function serialize(params) {
var pairs = [], currentKey, currentValue;
for(key in params) {
if(params.hasOwnProperty(key)) {
currentKey = key;
currentValue = params[key];
if(typeof currentValue === 'object') {
for(subKey in currentValue) {
if(currentValue.hasOwnProperty(subKey)) {
// If subKey is an integer, we have an array. In that case, use `person[]` instead of `person[0]`.
pairs.push(currentKey + '[' + (isNaN(subKey, 10) ? subKey : '') + ']=' + encodeURIComponent(currentValue[subKey]));
}
}
} else {
pairs.push(currentKey + '=' + encodeURIComponent(currentValue));
}
}
}
return pairs.join("&");
};
// Public interface.
$.querystring = function(param) {
if(typeof param === 'string') {
return parse(param);
} else {
return serialize(param);
}
};
// Adds a method to jQuery objects to get & querystring.
// $('#my_link').querystring(); // => {name: "Joe", job: "Plumber"}
// $('#my_link').querystring({name: 'Jack'}); // => Appends `?name=Jack` to href.
$.fn.querystring = function() {
var elm = $(this),
existingData,
newData = arguments[0] || {},
clearExisting = arguments[1] || false;
if(!elm.attr('href')) {
return;
}
existingData = parse(elm.attr('href'));
// Get the querystring & bail.
if(arguments.length === 0) {
return existingData;
}
// Set the querystring.
if(clearExisting) {
existingData = newData;
} else {
$.extend(existingData, newData);
}
elm.attr('href', elm.attr('href').split("?")[0] + "?" + serialize(existingData));
return elm;
};
})(jQuery);