我有两个过滤器:按位置和按类型。每次我都需要在不刷新页面的情况下更新散列url。我该怎么做呢?
链接结构示例: site.com/about#location=12#type=188
谢谢
发布于 2016-02-12 06:00:54
您可以使用window.location.hash调整url中的哈希。
window.location.hash = 'some value';发布于 2016-02-12 06:33:57
首先将散列按#拆分,以获得最多包含2项的数组:-
var hashes = window.location.hash.split('#');然后,您可以检查它们是否存在,如下所示:
var location = hashes.filter(function(i) { return i.startsWith('location'); });
var type = hashes.filter(function(i) { return i.startsWith('type'); });如果为location.length == 1且与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('#');https://stackoverflow.com/questions/35350870
复制相似问题