[英]How to refresh a page in JavaScript without POSTing?
I've seen this question already, but the top answers all suggest window.location.reload
. 我已经看到了这个问题 ,但是最上面的答案都建议window.location.reload
。 I've just discovered that this will actually do a POST
if your page was loaded with a POST. 我刚刚发现,这实际上会做一个POST
,如果你的网页内加入POST。
I reckon I could do window.location.href=window.location.href
but that won't work if there's a hashtag in the URL I'm told. 我认为我可以执行window.location.href=window.location.href
但是如果我被告知URL中有window.location.href=window.location.href
,那将无法正常工作。
So how can I get the browser to perform a GET on the current page, including query params (with or without the hash)? 那么,如何让浏览器在当前页面上执行GET,包括查询参数(带或不带哈希)?
您可以手动构造URL:
window.location.href=window.location.origin + window.location.pathname + window.location.hash;
function refresh() {
window.location.href = window.location.pathname + window.location.search;
};
You don't need the origin
(it doesn't work in old IE anyway). 您不需要origin
(无论如何,它在旧IE中不起作用)。 You should add .search
if you want to keep the query params. 如果要保留查询参数,则应添加.search
。 Don't add .hash
because it won't refresh if there is one. 不要添加.hash
因为如果有一个,它不会刷新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.