Javascript 从当前页面的URL中删除哈希

Javascript 从当前页面的URL中删除哈希,javascript,jquery,url,Javascript,Jquery,Url,我想从URL中删除散列以及散列之后的任何内容。例如,我可能有: http://example.com/question_1 …将其滑至问题1以显示错误消息。当用户的输入通过验证时,我需要从当前位置删除问题1 我已经尝试了所有这些,但没有一个对我有效: document.location.href.replacelocation.hash; window.location.hash.split[0]; window.location.hash.substr0,window.location.has

我想从URL中删除散列以及散列之后的任何内容。例如,我可能有:

http://example.com/question_1 …将其滑至问题1以显示错误消息。当用户的输入通过验证时,我需要从当前位置删除问题1

我已经尝试了所有这些,但没有一个对我有效:

document.location.href.replacelocation.hash; window.location.hash.split[0]; window.location.hash.substr0,window.location.hash.indexOf; 注意:我不仅仅想获取URL–我想将其从地址栏中删除。

尝试以下操作:使用.split拆分字符串,然后使用索引0读取数组中的第一个元素 var url='1〕http://example.comquestion_1'; var urlWithoutHash=url.split[0]; alerturlWithoutHash 在javascript中使用split var str=http://example.comquestion_1; alertstr.split[0] 试着这样做:

var currentPath = window.location.pathname;
var myUrl = currentPath.split("#")[0];


希望有帮助。

这将从uri中清除id选择器

location.hash = '';
使用.split,如图所示:

var str = "http://example.com#question_1";

alert((str.split("#")[0]);
var str = "http://example.com#question_1";

alert((str.substring(0,str.indexOf('#'))));
或使用.substring,如图所示:

var str = "http://example.com#question_1";

alert((str.split("#")[0]);
var str = "http://example.com#question_1";

alert((str.substring(0,str.indexOf('#'))));

是什么http://example.comquestion_1? 当前加载的url?请参见此处:可能重复感谢您的回答,但我不想获取url,我只想将其从我的url中删除barThanks for answer,但我不想获取url,我只想从我的URL中删除它baYou无法在不重新加载页面的情况下删除哈希,我认为这将是最接近实际从地址栏中删除它的方法: