San*_*254 27 html javascript url query-string
我有一个附加了长查询字符串的URL.页面加载后,我不需要查询字符串.所以我想从地址栏中删除查询字符串而不重新加载页面.
我试着parent.location.hash = '';和window.location.href = '/#'
他们并没有什么不同.
ger*_*iwi 36
正如其他人所说,您可以使用现代浏览器中的History API(IE10 +,FF4 +,Chrome5 +)来完成此操作.答案中没有完整的例子,所以我想要分享我的解决方案,因为我只需要做同样的事情:
history.pushState(null, "", location.href.split("?")[0]);
Run Code Online (Sandbox Code Playgroud)
如果您使用的是Modernizr,还可以检查History API是否可用,如下所示:
if (Modernizr.history) {
history.pushState(null, "", location.href.split("?")[0]);
}
Run Code Online (Sandbox Code Playgroud)
Que*_*tin 17
这在使用history api的现代浏览器中是可以实现的,但可能不是解决问题的最佳方案.
history.replaceState({}, 'some title', '/');
Run Code Online (Sandbox Code Playgroud)
听起来你最好处理数据,然后重定向到主页,而不是直接返回HTML文档.
由于您不想保留URL,因此对书签没有用处,因此很可能您最好不要发出POST请求.
这表明您应该使用POST-Redirect-GET模式.
小智 9
window.history.replaceState(null, null, window.location.pathname);
Run Code Online (Sandbox Code Playgroud)
如果不重新加载页面,你不能这样做,想象一下你是否可以在浏览器地址栏中放置你想要的东西?安全乐趣:)
虽然你现在可以使用新的历史API在HTML5(只适用于支持它的浏览器)上进行,但实际上,你的场景最好保证重写而不是包含它(似乎是大锤来破解).
至于你说你并不需要在页面加载后的查询字符串,你应该真的回来后,然后重定向到另一个网址你完成你的处理之后.