Skip to content

Instantly share code, notes, and snippets.

@greystate
Created January 20, 2011 10:03
Show Gist options
  • Save greystate/787685 to your computer and use it in GitHub Desktop.
Save greystate/787685 to your computer and use it in GitHub Desktop.
A couple of helpers...
function filterBy(selector) {
var c = selector.value, loc = document.location, q = loc.search;
q = addOrReplaceQueryStringParam(q, "category", c);
document.location = loc.pathname + q;
}
function gotoCategory(selector) {
var c = selector.value;
document.location = "/produkter?category=" + encodeURIComponent(c);
}
function addOrReplaceQueryStringParam(qs, name, value) {
qs = qs.indexOf("?") == 0 ? qs.substr(1) : qs;
var params = qs ? qs.split("&") : [ ];
if (qs.indexOf(name) >= 0) {
for (var i = params.length - 1; i >= 0; i--) {
if (params[i].indexOf(name) == 0) {
params[i] = name + "=" + encodeURIComponent(value);
}
}
} else {
params.push(name + "=" + encodeURIComponent(value));
}
return "?" + params.join("&");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment