mirror of
https://github.com/prymitive/karma
synced 2026-05-07 03:26:52 +00:00
Import code from internal repository (#1)
Import code from internal repository
This commit is contained in:
47
static/querystring.js
Normal file
47
static/querystring.js
Normal file
@@ -0,0 +1,47 @@
|
||||
var QueryString = (function() {
|
||||
|
||||
|
||||
parse = function() {
|
||||
var vars = [],
|
||||
hash;
|
||||
var q = document.URL.split('?')[1];
|
||||
if (q != undefined) {
|
||||
q = q.split('&');
|
||||
for (var i = 0; i < q.length; i++) {
|
||||
hash = q[i].split('=');
|
||||
vars.push(hash[1]);
|
||||
vars[hash[0]] = hash[1];
|
||||
}
|
||||
}
|
||||
return vars;
|
||||
}
|
||||
|
||||
|
||||
update = function(key, value) {
|
||||
/* https://gist.github.com/excalq/2961415 */
|
||||
var baseUrl = [location.protocol, '//', location.host, location.pathname].join(''),
|
||||
urlQueryString = document.location.search,
|
||||
newParam = key + '=' + value,
|
||||
params = '?' + newParam;
|
||||
|
||||
// If the "search" string exists, then build params from it
|
||||
if (urlQueryString) {
|
||||
keyRegex = new RegExp('([\?&])' + key + '[^&]*');
|
||||
|
||||
// If param exists already, update it
|
||||
if (urlQueryString.match(keyRegex) !== null) {
|
||||
params = urlQueryString.replace(keyRegex, "$1" + newParam);
|
||||
} else { // Otherwise, add it to end of query string
|
||||
params = urlQueryString + '&' + newParam;
|
||||
}
|
||||
}
|
||||
window.history.replaceState({}, "", baseUrl + params);
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
Parse: parse,
|
||||
Set: update
|
||||
}
|
||||
|
||||
}());
|
||||
Reference in New Issue
Block a user