[英]Removing hash from URL using JS/jQuery
I have the following code: 我有以下代码:
if(window.location.hash){
var hash = window.location.hash.substring(1);
}
var hash = window.location.hash.substring(1);
if(hash.length > 0 && hash.search('^y([0-9]+)g([0-9]+)$') >= 0)
{
$('#recs').removeClass('closed');
$('#rec_select_year').val(hash.match('y([0-9]+)')[1]);
$('#rec_g_select').val(hash.match('g([0-9]+)')[1]);
$('html, body').animate({
scrollTop: $('#docs').offset().top + 200
}, 0);
changeDocuments();
}
else
{
changeDocuments();
}
$('.filter').change(function() {
updateDocumentList();
var years = $('#rec_select_year').val();
var groups = $('#rec_g_select').val();
hash = 'y' + years + 'g' + groups;
});
I'm having difficulties with removing hash from URL. 我在从网址中删除哈希值时遇到困难。 I need to change url to this http://codify.org/group/y2018g7
我需要将网址更改为此http://codify.org/group/y2018g7
For that reason, I used substring() method to remove the hash, however it affects to the url and it changes to http://codify.org/group/ , which basically removes the end of URL ../ y2018g7 因此,我使用substring()方法删除了哈希,但是它影响了url,并且更改为http://codify.org/group/ ,基本上删除了URL的末尾../ y2018g7
How can I change hash to to display an URL which is http://codify.org/group/y2018g7 ? 我如何更改哈希值以显示http://codify.org/group/y2018g7的URL?
Use RegExp
to replace #
from url
like below. 使用
RegExp
替换url
#
,如下所示。
var url = "codify.org/group/#y2018g7"; var regx = "/#/g"; var newUrl= url.replace(eval(regx), ''); console.log(newUrl);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Note: In above RegExp The g modifier is for global match (find all matches rather than stopping after the first match) 注意:在上述RegExp中,g修饰符用于全局匹配(查找所有匹配项,而不是在第一个匹配项后停止)
如果要用空字符串替换所有哈希,这将起作用。
window.location.href.split("#").join('')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.