如何使用JavaScript重新加载页面?
我需要一个在所有浏览器中都可用的方法。
简短版:
location.reload();
长版本(与短版本相同):
window.location.reload();
一些浏览器支持一个可选的布尔参数,可以强制清除缓存(类似于Ctrl+Shift+R),但这是非标准的、支持和文档化都很差,通常不应该使用:
//Force a hard reload to clear the cache if supported by the browser
window.location.reload(true);
window.location.replace(window.location.pathname + window.location.search + window.location.hash);
// does not create a history entry
window.location.href = window.location.pathname + window.location.search + window.location.hash;
// creates a history entry
location.reload()
和 window.location.reload()
有什么区别? - Raptorlocation
与window.location
是相同的,因为window
是全局对象。 - Lekensteynwindow.location.reload();
以提高可读性,因为 location
可能是一个局部变量 - 而通常会避免使用名称为 window
的变量。 - Yetireturn false;
并不是必需的:因为 reload
覆盖了任何进一步的处理,对吗? - ToolmakerSteve我正在寻找与使用POST请求检索的页面重新加载相关的一些信息,例如在提交 method =“post”
表单后。
要重新加载页面并保留POST数据,请使用:
window.location.reload();
重新加载页面并放弃POST数据(执行GET请求),请使用以下代码:location.reload(true)
window.location.href = window.location.href;
希望这能帮助到其他寻找同样信息的人。
POST
请求检索的文档的参考。 - Christopher SchultzGET
和POST
方法。 - Hassan Baig#
,则window.location.href = window.location.href
不会重新加载页面。如果您不需要它,可以删除散列(#
):window.location.href = window.location.href.split('#')[0];
。 - Roland Starkewindow.location.reload();
完成此任务。虽然有很多方法可以实现此操作,但我认为使用JavaScript重新加载相同的文档是适当的方法。以下是解释:window.location
对象可用于
window
: 在JavaScript中表示浏览器中打开的窗口。
location
: 在JavaScript中包含有关当前URL的信息。
location
对象类似于 window
对象的一部分,并通过 window.location
属性调用。
location
对象有三种方法:
assign()
: 用于加载新文档reload()
: 用于重新加载当前文档replace()
: 用于替换当前文档为新文档reload()
,因为它可以帮助我们重新加载相同的文档。window.location.reload();
。true
参数传递给 location.reload()
。此方法与所有主要浏览器兼容,包括IE、Chrome、Firefox、Safari和Opera。replace()
原来就是我正在寻找的解决方案,因为我需要通过轻微更改查询字符串重新加载页面。 - Bernard Hymmen这对我很有效:
function refresh() {
setTimeout(function () {
location.reload()
}, 100);
}
要使用JavaScript重新加载页面,请使用:
window.location.reload();
window.location.reload(true);
如果在页面的开头没有其他条件来限制代码运行,那么页面将会加载并且不断重载直到你关闭浏览器。
location.href = location.href;
location.reload()
? - Matthiashistory.go()
location.reload(forceReload: boolean)
现在被弃用了(仅限于true
/false
参数),尽管它可能仍然可以工作。 - Andrewnocache=' + (new Date()).getTime()
。 - StanE