これにより、重複したフィルターが削除されます。
function removeDupFilters(str) {
var pos = str.search(/\/.*?$/), path, items, map = {}, i;
if (pos !== -1) {
path = str.substr(pos + 1);
items = path.split("+");
for (i = 0; i < items.length; i++) {
map[items[i]] = true;
}
items = [];
for (i in map) {
items.push(i);
}
return str.substr(0, pos + 1) + items.join("+");
}
return str;
}
実際のデモ: http://jsfiddle.net/jfriend00/ntb8f/
コードで使用すると、次のようになります。
$('#coll-filter li a').one('click', function (e) {
var url = removeDupFilters(window.location.href + '+' + $(this).attr('href'));
if (url !== window.location.href) {
// go to the new URL
window.location.href = url;
}
e.preventDefault();
});
または、次のようにその関数を使用せずに、現在の URL を直接確認することもできます。
$('#coll-filter li a').one('click', function (e) {
var filter = $(this).attr('href');
var re = new RegExp("/|\\+" + filter + "$|\\+", "i");
if (!re.test(window.location.href)) {
// go to the new URL
window.location.href = window.location.href + "+" + filter;
}
e.preventDefault();
});