Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

js截取url的问题,不要#后面的

目前我知道的js获取携带参数的url的方式是
window.location.search

但是假如说有个链接是 www.xxxxxx.com?aaa=bbb&ccc=ddd#ok

取得的就是?aaa=bbb&ccc=ddd#ok
我不想要这个#后面的这个hash值,能去掉不,还是必须要用正则才能去掉?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

location 对象有一个 hash 属性,存储着 URL 中以 # 开头的字符串,所以不一定要用正则替换,直接匹配替换也可以:

var nohash = window.location.href.replace(window.location.hash, '');

参考:http://www.w3school.com.cn/js...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...