-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquerystring.js
More file actions
40 lines (35 loc) · 1.25 KB
/
Copy pathquerystring.js
File metadata and controls
40 lines (35 loc) · 1.25 KB
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
(function(window) {
var _queryString = {};
/**
* IFFE function to parse query string and store values in _queryString
* pulled search regex from the page below: /([^&=]+)=?([^&]*)/g
* @see http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript
*
* @param {String} currentUrl
*/
(function(currentUrl){
var query = currentUrl.split('?')[1] || '',
search = /([^&=]+)=?([^&]*)/g,
paramParts;
for(paramParts = search.exec(query); paramParts; paramParts = search.exec(query)) {
_queryString[paramParts[1]] = decodeURIComponent(paramParts[2].replace(/\+/g, ' '));
}
}(window.location.href));
/**
* Accessor method to retrieve querystring values or the entire set of values
* @param {String} name
* @return {Mixed} Returns the value of the named parameter passed in, or null.
* If called with nothing passed in, it returns the entire _queryString object
*/
var querystring = function querystring(name) {
if(arguments.length > 0) {
if(typeof name === 'string') {
return (_queryString.hasOwnProperty(name)) ? _queryString[name] : null;
}else{
throw "The parameter name must be a string";
}
}
return _queryString;
};
window.querystring = querystring;
}(window, undefined));