2016-02-11 61 views
1

我有2个过滤器:按位置和类型。每次我需要更新散列URL而不刷新页面。我怎样才能做到这一点?例如用于排序数据的多个(2)哈希URL参数

链接结构: site.com/about#location=12#type=188

感谢

+0

对不起,我不能给你一个明确的答案,但你想要做的是使用HTML5历史API https://css-tricks.com/using-the-html5-history-api/ –

+0

我会用'site.com/about#{“filters”:{“location”:12,“type”:188}}'然后你可以很容易地阅读和操纵变量 – DelightedD0D

回答

0

可以使用window.location.hash调整在URL中的哈希值。

window.location.hash = 'some value'; 
+0

这不是我要找的...我的链接看起来像site.com/about#location=12#type=188 – vol4ikman

+0

您可以使用这种方法。 'window.location.hash ='location = 12#type = 188';'或者我误解了? –

+0

是的,但我怎么能检查散列位置或类型已经存在于当前的网址之前,我添加新我想散列参数之一? – vol4ikman

0

第一分由#哈希起床到2项的数组: - :

var location = hashes.filter(function(i) { return i.startsWith('location'); }); 
var type = hashes.filter(function(i) { return i.startsWith('type'); }); 

如果location.length == 1 -

var hashes = window.location.hash.split('#'); 

那么如果它们存在像你可以检查并且与type一样,那么它们被设置。

,那么你可以更新/更换,如: -

location[0] = "location=" + 11; 
type[0] = "type=" + 190; 

最简单的方式加入他们重新走到一起,包住一个心不是一套是将它们添加到一个新的阵列和join

var arr = []; 

if(location.length == 1) arr.push(location[0]); 
if(type.length == 1) arr.push(type[0]); 

window.location.hash = arr.join('#');