I have a website where visitors can select filters to find a vacation. When you navigate to a country there are four tiles atop the page with regions or cities. I'd like to take parameters from the URL and attach them to the new url when you click on any of these tiles. This is my code:
var arr = window.location.search.split('?')[1].split('&');
var filters = "";
$(arr).each(function (i) {
if (arr[i].indexOf("geo=") === -1 && arr[i].indexOf("order=") === -1 && arr[i].indexOf("theme=") === -1) {
filters = filters.concat("&", arr[i]);
}
});
$("a.vak-card__content.vak-card__content--overlaid").each(function (i) {
var href = $("a.vak-card__content.vak-card__content--overlaid:eq(" + i + ")").attr("href");
if (href.indexOf("geo=") != -1) {
href = href.split("&search=yes")[0].concat(filters);
$("a.vak-card__content.vak-card__content--overlaid:eq(" + i + ")").attr("href", href);
}
});
The only problem is that above code doesn't take double values into consideration. The challenge is to determine filters from current URL both with a value and without a value, then to exclude filters without a value for all tiles, compare with filters from URL and finally if a filter is not included in the URL to add it to the tile (new URL). I know it sounds convoluted, I wish I could explain it more clearly.
The filters I'm talking about (parameters in the URL):
- geo=
theme=
airports=
duration=
order=
distanceToBeach= etc...