2016-02-11 30 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('#');