我有URL: http://example.com#something,我如何删除#一些,而不导致页面刷新?
我尝试了以下解决方案:
window.location.hash = '';
但是,这不会从URL中删除散列符号#。
我有URL: http://example.com#something,我如何删除#一些,而不导致页面刷新?
我尝试了以下解决方案:
window.location.hash = '';
但是,这不会从URL中删除散列符号#。
当前回答
<script type="text/javascript">
var uri = window.location.toString();
if (uri.indexOf("?") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("?"));
window.history.replaceState({}, document.title, clean_uri);
}
</script>
把这个代码放在头部分
其他回答
<script type="text/javascript">
var uri = window.location.toString();
if (uri.indexOf("?") > 0) {
var clean_uri = uri.substring(0, uri.indexOf("?"));
window.history.replaceState({}, document.title, clean_uri);
}
</script>
把这个代码放在头部分
您可以将哈希替换为null
var urlWithoutHash = document.location.href.replace(location.hash , "" );
我觉得这样更安全
if (window.history) {
window.history.pushState('', document.title, window.location.href.replace(window.location.hash, ''));
} else {
window.location.hash = '';
}
你可以这样做:
history.replaceState({}, document.title, window.location.href.split('#')[0]);
试试下面的方法:
window.history.back(1);