Last active
December 12, 2021 12:53
-
-
Save pduey/2764606 to your computer and use it in GitHub Desktop.
Javascript to add/remove a query string parameter/value pair
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// pduey: a couple of public and private functions to add or remove a query string parameter + value pair. | |
// | |
// I like this implementation because it transforms the query string into a coherent hash with unique keys | |
// and an Array for the values, so it handles repeated query string parameter names, which are meant to be | |
// interpreted as Arrays, yo. The "private" functions could, of course, be handy for other uses. | |
// | |
// The function names include "search" because they are derived from window.location.search. | |
// Ideally, I'd attach these to the window object, as in, window.location.prototype. | |
// | |
// pduey: add a variable/value pair to the current query string and return updated href | |
function search_add(name, value) { | |
new_search_hash = search_to_hash(); | |
if ( ! (decodeURIComponent(name) in new_search_hash)) { | |
new_search_hash[decodeURIComponent(name)] = new Array(); | |
} | |
new_search_hash[decodeURIComponent(name)].push(decodeURIComponent(value)); | |
return hash_to_search(new_search_hash); | |
} | |
// pduey: remove a variable/value pair from the current query string and return updated href | |
function search_remove(name, value) { | |
new_search_hash = search_to_hash(); | |
if (new_search_hash[name].indexOf(value) >= 0) { | |
new_search_hash[name].splice(new_search_hash[name].indexOf(value), 1); | |
if (new_search_hash[name].length == 0) { | |
delete new_search_hash[name]; | |
} | |
} | |
return hash_to_search(new_search_hash); | |
} | |
function search_to_hash() { | |
var h={}; | |
if (window.location.search == undefined || window.location.search.length < 1) { return h;} | |
q = window.location.search.slice(1).split('&'); | |
for (var i = 0; i < q.length; i++) { | |
key_val = q[i].split('='); | |
// replace '+' (alt space) char explicitly since decode does not | |
hkey = decodeURIComponent(key_val[0]).replace(/\+/g,' '); | |
hval = decodeURIComponent(key_val[1]).replace(/\+/g,' ');; | |
if (h[hkey] == undefined) { | |
h[hkey] = new Array(); | |
} | |
h[hkey].push(hval); | |
} | |
return h; | |
} | |
function hash_to_search(h) { | |
search = new String("?"); | |
for (var k in h) { | |
for (var i = 0; i < h[k].length; i++) { | |
search += search == "?" ? "" : "&"; | |
search += encodeURIComponent(k) + "=" + encodeURIComponent(h[k][i]); | |
} | |
} | |
return search; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thank you man !