[英]How do I reload the page without the query parameters?
假设我想重新加载www.domain.com/abc?num=4
但是我想重新加载www.domain.com/abc
(没有问号之后的所有内容)
window.location = window.location.href.split("?")[0];
有几种方法可以解决它:
window.location = window.location.href.split("?")[0];
或者,或者:
window.location = window.location.pathname;
这是最好最简单的方法,
// similar to HTTP redirect
window.location.replace(location.pathname);
我通常会尽量避免进行不必要的函数调用,特别是当我需要的信息已经由DOM提供给我时。 也就是说,这可能是客观上最好的解决方案:
window.location = window.location.pathname + window.location.hash;
正如用户qbert65536 的评论中所指出的,有许多流行的现代框架使用散列来表示视图和路径,这就是为什么单独使用window.location.pathname
是不够的。
试试这个Javascript:
location = location.pathname;
我假设用户按下按钮以进行此刷新。 如果按钮位于表单元素内,请确保按钮类型设置为“按钮”(下面的示例):
<button type='button' id='mybutton'>Button Name</button>
如果未设置类型,则默认为type ='submit',并将作为表单提交按钮,从而在重新加载时为您提供URL中的所有额外参数。
然后,这是一个简单的JavaScript刷新调用:
window.location.reload();
简单,简单,刚刚测试:
window.location.href = location.pathname;
top.location.href = top.location.protocol+top.location.host+top.location.pathname
document.location = String(document.location).replace(/\?.*$/, '');
你可以使用document.URL和split函数到你想要加载的url并使用方法window.location.href来加载页面
试试这个:
var url = 'www.domain.com/abc?num=4';
alert(url.split('?')[0]);
var url="www.domain.com/abc?num=4";
window.location.href=url.replace(/^([^\?]+)(\??.*)$/gi,"$1");
我真的不明白你的问题,但也许这会对你有所帮助:
<input type="button" value="Reload Page" onClick="window.location.href=www.domain.com/abc">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.