I am creating a filter based on size, color etc. So it is possible to select multiple items. When I click each item it should append to current URL. Please check the below URL,
If parameter not exists in URL
http://example.com/category-3?tracker=Q2F0ZWdvcnkgMw&color=White
If parameter exists and selected same color
http://example.com/category-3?tracker=Q2F0ZWdvcnkgMw&color=White
If parameter exists and selected multiple colors
http://example.com/category-3?tracker=Q2F0ZWdvcnkgMw&color=White-Black
How to implement the same using jQuery. I have tried using below code,
function setGetParameter(paramName, paramValue) {
var url = window.location.href;
var hash = location.hash;
url = url.replace(hash, '');
if (url.indexOf(paramName + "=") >= 0) {
var prefix = url.substring(0, url.indexOf(paramName + "="));
var suffix = url.substring(url.indexOf(paramName + "="));
suffix = suffix.substring(suffix.indexOf("=") + 1);
suffix = (suffix.indexOf("&") >= 0) ? suffix.substring(suffix.indexOf("&")) : "";
url = prefix + paramName + "=" + paramValue + suffix + "-";
} else {
if (url.indexOf("?") < 0)
url += "?" + paramName + "=" + paramValue + "-";
else
url += "&" + paramName + "=" + paramValue + "-";
}
window.location.href = url + hash;
}
The above code works with single parameter value. For multiple values how to alter the above code.